gitlab-org--gitlab-foss/app/serializers
2021-10-04 12:11:58 +00:00
..
acts_as_taggable_on use lazy ajax filter dropdown for runner tags 2019-02-27 20:19:49 +01:00
admin Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
analytics/cycle_analytics Add latest changes from gitlab-org/gitlab@master 2021-07-07 18:08:30 +00:00
ci Add latest changes from gitlab-org/gitlab@master 2021-08-03 15:10:03 +00:00
concerns Add latest changes from gitlab-org/gitlab@master 2021-07-28 15:09:57 +00:00
error_tracking Add latest changes from gitlab-org/gitlab@master 2020-01-16 09:08:46 +00:00
evidences Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
feature_flags Add latest changes from gitlab-org/gitlab@master 2020-09-23 09:10:07 +00:00
group_link Add latest changes from gitlab-org/gitlab@master 2021-08-19 00:11:16 +00:00
import Add latest changes from gitlab-org/gitlab@master 2020-12-15 09:10:00 +00:00
integrations Add latest changes from gitlab-org/gitlab@master 2021-07-28 12:10:41 +00:00
jira_connect Add latest changes from gitlab-org/gitlab@master 2021-07-28 18:10:23 +00:00
merge_requests Add latest changes from gitlab-org/gitlab@master 2021-04-06 12:09:21 +00:00
projects/serverless Add latest changes from gitlab-org/gitlab@master 2020-02-11 18:08:58 +00:00
rollout_statuses Add latest changes from gitlab-org/gitlab@master 2020-11-30 18:09:46 +00:00
serverless Add latest changes from gitlab-org/gitlab@master 2020-02-27 15:09:24 +00:00
accessibility_error_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-05-08 03:09:54 +00:00
accessibility_reports_comparer_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-05-08 03:09:54 +00:00
accessibility_reports_comparer_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-05-08 03:09:54 +00:00
analytics_build_entity.rb
analytics_build_serializer.rb
analytics_commit_entity.rb
analytics_commit_serializer.rb
analytics_generic_serializer.rb
analytics_issue_entity.rb Fix error on project name 2019-08-05 21:15:00 +00:00
analytics_issue_serializer.rb
analytics_merge_request_entity.rb Add latest changes from gitlab-org/gitlab@master 2019-11-19 12:06:00 +00:00
analytics_merge_request_serializer.rb
analytics_stage_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-06-02 09:09:46 +00:00
analytics_stage_serializer.rb
analytics_summary_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-05-12 15:10:33 +00:00
analytics_summary_serializer.rb
award_emoji_entity.rb
base_discussion_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-02-18 03:09:22 +00:00
base_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-09-02 12:10:35 +00:00
blob_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
board_serializer.rb Use serialization for project boards 2019-06-06 22:13:14 +12:00
board_simple_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
build_action_entity.rb
build_artifact_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-04-12 03:09:13 +00:00
build_coverage_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-09-01 18:10:48 +00:00
build_details_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-22 06:08:43 +00:00
build_metadata_entity.rb
build_serializer.rb
build_trace_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-07-01 15:08:45 +00:00
build_trace_serializer.rb Add latest changes from gitlab-org/gitlab@master 2019-10-17 15:06:17 +00:00
cluster_application_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-06-15 12:10:11 +00:00
cluster_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-14 03:10:11 +00:00
cluster_error_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-05 21:09:40 +00:00
cluster_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-09-18 21:10:11 +00:00
codequality_degradation_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-12-07 09:09:42 +00:00
codequality_reports_comparer_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-12-07 09:09:42 +00:00
codequality_reports_comparer_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-12-07 09:09:42 +00:00
cohort_activity_month_entity.rb
cohort_entity.rb
cohorts_entity.rb
cohorts_serializer.rb
commit_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-03-23 15:09:36 +00:00
container_repositories_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-02-06 21:08:48 +00:00
container_repository_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-10-02 12:09:03 +00:00
container_tag_entity.rb Add Container Registry API 2019-01-25 13:13:48 +01:00
container_tags_serializer.rb
context_commits_diff_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-04-29 15:10:07 +00:00
current_board_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
current_board_serializer.rb Split and prepend CurrentBoardEntity 2019-07-12 06:31:17 +00:00
current_user_entity.rb
deploy_key_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-07-15 12:09:26 +00:00
deploy_key_serializer.rb
deploy_keys_project_entity.rb
deployment_cluster_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-02-14 09:08:43 +00:00
deployment_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-18 15:10:46 +00:00
deployment_serializer.rb Replace finished_at with deployed_at for Deployment entity 2019-08-20 12:39:19 +03:00
detailed_status_entity.rb Run rubocop -a 2019-03-13 13:42:43 +00:00
diff_file_base_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-12-15 00:10:07 +00:00
diff_file_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-28 15:09:57 +00:00
diff_file_metadata_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-28 15:09:57 +00:00
diff_line_entity.rb Allow suggesting single line changes in diffs 2018-12-13 19:17:19 +00:00
diff_line_parallel_entity.rb
diff_line_serializer.rb
diff_viewer_entity.rb Replaced part of diff file properties with diff viewer 2019-02-15 17:56:50 +00:00
diffs_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-21 21:10:10 +00:00
diffs_metadata_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-28 15:09:57 +00:00
diffs_metadata_serializer.rb Add latest changes from gitlab-org/gitlab@master 2019-10-10 12:06:19 +00:00
diffs_serializer.rb
discussion_diff_file_entity.rb Remove unused data from discussions endpoint 2018-12-06 17:07:49 -08:00
discussion_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-03-26 15:09:18 +00:00
discussion_serializer.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
draft_note_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-05-28 09:08:05 +00:00
draft_note_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-05-28 09:08:05 +00:00
entity_date_helper.rb Add latest changes from gitlab-org/gitlab@master 2019-09-18 14:02:45 +00:00
entity_request.rb
environment_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-18 15:10:46 +00:00
environment_serializer.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
environment_status_entity.rb Add latest changes from gitlab-org/gitlab@master 2019-11-29 09:06:31 +00:00
environment_status_serializer.rb
feature_flag_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-09-23 12:11:29 +00:00
feature_flag_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-09-23 09:10:07 +00:00
feature_flag_summary_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-09-23 09:10:07 +00:00
feature_flag_summary_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-09-23 09:10:07 +00:00
feature_flags_client_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-09-23 09:10:07 +00:00
feature_flags_client_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-09-23 09:10:07 +00:00
fork_namespace_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-06-08 15:10:00 +00:00
fork_namespace_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-07-09 00:09:11 +00:00
group_basic_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-14 03:10:00 +00:00
group_child_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-08-24 18:10:53 +00:00
group_child_serializer.rb
group_deploy_key_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-14 03:10:00 +00:00
group_deploy_key_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-08-14 03:10:00 +00:00
group_deploy_keys_group_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-14 03:10:00 +00:00
group_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-08-24 18:10:53 +00:00
group_issuable_autocomplete_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-06 12:10:38 +00:00
group_issuable_autocomplete_serializer.rb Add latest changes from gitlab-org/gitlab@master 2021-05-06 12:10:38 +00:00
group_serializer.rb
issuable_entity.rb
issuable_sidebar_basic_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
issuable_sidebar_extras_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-08-24 06:08:55 +00:00
issuable_sidebar_todo_entity.rb Refactor sidebar to use data from serializer 2018-12-19 17:58:53 +08:00
issue_board_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-06-15 21:10:04 +00:00
issue_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
issue_serializer.rb Code style changes and refactor 2018-12-20 07:28:28 +08:00
issue_sidebar_basic_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
issue_sidebar_extras_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
job_artifact_report_entity.rb Add latest changes from gitlab-org/gitlab@master 2019-11-05 18:08:34 +00:00
job_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-14 12:10:58 +00:00
job_group_entity.rb
label_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-10-14 12:08:58 +00:00
label_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-10-14 09:08:46 +00:00
lfs_file_lock_entity.rb
lfs_file_lock_serializer.rb
linked_issue_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-21 00:10:44 +00:00
linked_project_issue_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-21 00:10:44 +00:00
linked_project_issue_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-08-21 00:10:44 +00:00
member_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-06-01 21:10:06 +00:00
member_serializer.rb Add latest changes from gitlab-org/gitlab@master 2021-06-14 12:10:13 +00:00
member_user_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
merge_request_basic_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-02-05 12:09:31 +00:00
merge_request_create_entity.rb
merge_request_create_serializer.rb
merge_request_current_user_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-12-04 03:09:49 +00:00
merge_request_diff_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-06-28 09:08:19 +00:00
merge_request_for_pipeline_entity.rb Update pipeline detail view to accommodate post-merge pipelines 2019-03-19 23:19:27 +07:00
merge_request_metrics_entity.rb
merge_request_noteable_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-06-16 18:09:01 +00:00
merge_request_poll_cached_widget_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-27 09:10:08 +00:00
merge_request_poll_widget_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-27 09:10:08 +00:00
merge_request_serializer.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
merge_request_sidebar_basic_entity.rb Fix comments related to BE part 2019-08-20 15:15:18 -05:00
merge_request_sidebar_extras_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-02-05 12:09:31 +00:00
merge_request_user_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
merge_request_widget_commit_entity.rb Allow custom squash commit messages 2019-02-06 12:33:11 +00:00
merge_request_widget_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-10-04 12:11:58 +00:00
move_to_project_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-11-11 12:09:06 +00:00
move_to_project_serializer.rb
namespace_basic_entity.rb Improve the GitHub and Gitea import feature table interface 2019-02-13 00:15:57 +00:00
namespace_serializer.rb Improve the GitHub and Gitea import feature table interface 2019-02-13 00:15:57 +00:00
note_attachment_entity.rb
note_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
note_user_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
paginated_diff_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-21 21:10:10 +00:00
paginated_diff_serializer.rb Add latest changes from gitlab-org/gitlab@master 2019-10-09 21:06:24 +00:00
pipeline_details_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-12 12:10:24 +00:00
pipeline_serializer.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
project_entity.rb
project_import_entity.rb Improve the GitHub and Gitea import feature table interface 2019-02-13 00:15:57 +00:00
project_mirror_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
project_mirror_serializer.rb
project_note_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-09-16 15:09:32 +00:00
project_note_serializer.rb
project_serializer.rb Improve the GitHub and Gitea import feature table interface 2019-02-13 00:15:57 +00:00
prometheus_alert_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-07 06:09:47 +00:00
prometheus_alert_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-03-23 12:09:47 +00:00
prometheus_metric_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-04-07 12:09:34 +00:00
prometheus_metric_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-04-07 12:09:34 +00:00
README.md Add latest changes from gitlab-org/gitlab@master 2021-04-01 00:09:32 +00:00
release_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-12 12:10:25 +00:00
release_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-08-12 12:10:25 +00:00
remote_mirror_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-04-30 21:09:47 +00:00
request_aware_entity.rb
review_app_setup_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-01-09 12:08:03 +00:00
review_app_setup_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-01-09 12:08:03 +00:00
rollout_status_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-11-30 18:09:46 +00:00
route_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-04-15 18:09:36 +00:00
route_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-04-15 18:09:36 +00:00
runner_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-04-16 00:09:09 +00:00
service_event_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-07-06 21:07:50 +00:00
service_event_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-05-14 15:08:14 +00:00
service_field_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-03-25 12:09:19 +00:00
service_field_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-06-03 09:08:47 +00:00
stage_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-06-22 15:09:27 +00:00
stage_serializer.rb
suggestion_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-07-20 18:09:27 +00:00
suggestion_serializer.rb Fixed test specs 2019-04-04 13:08:34 +00:00
test_case_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-07 12:10:27 +00:00
test_report_entity.rb Add latest changes from gitlab-org/gitlab@master 2019-10-08 09:06:09 +00:00
test_report_serializer.rb Add latest changes from gitlab-org/gitlab@master 2019-10-08 09:06:09 +00:00
test_report_summary_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-11 21:10:09 +00:00
test_report_summary_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-07-09 15:08:59 +00:00
test_reports_comparer_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-02-17 12:09:20 +00:00
test_reports_comparer_serializer.rb
test_suite_comparer_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-03-01 12:11:29 +00:00
test_suite_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-09-01 21:10:21 +00:00
test_suite_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-07-09 15:08:59 +00:00
test_suite_summary_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-03-12 21:09:12 +00:00
time_trackable_entity.rb
trigger_variable_entity.rb Add specs for TriggerVariableEntity 2018-12-07 17:07:58 +01:00
triggered_pipeline_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-08-13 15:10:03 +00:00
user_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
user_preference_entity.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
user_serializer.rb Add latest changes from gitlab-org/gitlab@master 2021-05-11 21:10:21 +00:00
web_ide_terminal_entity.rb Add latest changes from gitlab-org/gitlab@master 2020-05-25 15:07:58 +00:00
web_ide_terminal_serializer.rb Add latest changes from gitlab-org/gitlab@master 2020-05-25 15:07:58 +00:00

Serializers

This is a documentation for classes located in app/serializers directory.

In GitLab, we use grape-entities, accompanied by a serializer, to convert a Ruby object to its JSON representation.

Serializers are typically used in controllers to build a JSON response that is usually consumed by a frontend code.

Why using a serializer is important?

Using serializers, instead of to_json method, has several benefits:

  • it helps to prevent exposure of a sensitive data stored in the database
  • it makes it easier to test what should and should not be exposed
  • it makes it easier to reuse serialization entities that are building blocks
  • it makes it easier to move complexity from controllers to easily testable classes
  • it encourages hiding complexity behind intentions-revealing interfaces
  • it makes it easier to take care about serialization performance concerns
  • it makes it easier to reduce merge conflicts between CE -> EE
  • it makes it easier to benefit from domain driven development techniques

What is a serializer?

A serializer is a class that encapsulates all business rules for building a JSON response using serialization entities.

It is designed to be testable and to support passing additional context from the controller.

What is a serialization entity?

Entities are lightweight structures that allow to represent domain models in a consistent and abstracted way, and reuse them as building blocks to create a payload.

Entities located in app/serializers are usually derived from a Grape::Entity class.

Serialization entities that do require to have a knowledge about specific elements of the request, need to mix RequestAwareEntity in.

A serialization entity usually maps a domain model class into its JSON representation. It rarely happens that a serialization entity exists without a corresponding domain model class. As an example, we have an Issue class and a corresponding IssueSerializer.

Serialization entities are designed to reuse other serialization entities, which is a convenient way to create a multi-level JSON representation of a piece of a domain model you want to serialize.

See documentation for Grape Entities for more details.

How to implement a serializer?

Base implementation

In order to effectively implement a serializer it is necessary to create a new class in app/serializers. See existing serializers as an example.

A new serializer should inherit from a BaseSerializer class. It is necessary to specify which serialization entity will be used to serialize a resource.

class MyResourceSerializer < BaseSerializer
  entity MyResourceEntity
end

The example above shows how a most simple serializer can look like.

Given that the entity MyResourceEntity exists, you can now use MyResourceSerializer in the controller by creating an instance of it, and calling MyResourceSerializer#represent(resource) method.

Note that a resource can be either a single object, an array of objects or an ActiveRecord::Relation object. A serialization entity should be smart enough to accurately represent each of these.

It should not be necessary to use Enumerable#map, and it should be avoided from the performance reasons.

Choosing what gets serialized

It often happens that you might want to use the same serializer in many places, but sometimes the intention is to only expose a small subset of object's attributes in one place, and a different subset in another.

BaseSerializer#represent(resource, opts = {}) method can take an additional hash argument, opts, that defines what is going to be serialized.

BaseSerializer will pass these options to a serialization entity. See how it is documented in the upstream project.

With this approach you can extend the serializer to respond to methods that will create a JSON response according to your needs.

class PipelineSerializer < BaseSerializer
  entity Ci::PipelineEntity

  def represent_details(resource)
    represent(resource, only: [:details])
  end

  def represent_status(resource)
    represent(resource, only: [:status])
  end
end

It is possible to use only and except keywords. Both keywords do support nested attributes, like except: [:id, { user: [:id] }].

Passing only and except to the represent method from a controller is possible, but it defies principles of encapsulation and testability, and it is better to avoid it, and to add a specific method to the serializer instead.

Reusing serialization entities from the API

Public API in GitLab is implemented using Grape.

Under the hood it also uses Grape::Entity classes. This means that it is possible to reuse these classes to implement internal serializers.

You can either use such entity directly:

class MyResourceSerializer < BaseSerializer
  entity API::Entities::SomeEntity
end

Or derive a new serialization entity class from it:

class MyEntity < API::Entities::SomeEntity
  include RequestAwareEntity

  unexpose :something
end

It might be a good idea to write specs for entities that do inherit from the API, because when API payloads are changed / extended, it is easy to forget about the impact on the internal API through a serializer that reuses API entities.

It is usually safe to do that, because API entities rarely break backward compatibility, but additional exposure may have a performance impact when API gets extended significantly. Write tests that check if only necessary data is exposed.

How to write tests for a serializer?

Like every other class in the project, creating a serializer warrants writing tests for it.

It is usually a good idea to test each public method in the serializer against a valid payload. BaseSerializer#represent returns a hash, so it is possible to use usual RSpec matchers like include.

Sometimes, when the payload is large, it makes sense to validate it entirely using match_response_schema matcher along with a new fixture that can be stored in spec/fixtures/api/schemas/. This matcher is using a json-schema gem, which is quite flexible, see a documentation for it.

How to use a serializer in a controller?

Once a new serializer is implemented, it is possible to use it in a controller.

Create an instance of the serializer and render the response.

def index
  format.json do
    render json: MyResourceSerializer
      .new(current_user: @current_user)
      .represent_details(@project.resources)
  end
end

If it is necessary to include additional information in the payload, it is possible to extend what is going to be rendered, the usual way:

def index
  format.json do
    render json: {
      resources: MyResourceSerializer
        .new(current_user: @current_user)
        .represent_details(@project.resources),
      count: @project.resources.count
    }
  end
end

Note that in these examples an additional context is being passed to the serializer (current_user: @current_user).

How to pass an additional context from the controller?

It is possible to pass an additional context from a controller to a serializer and each serialization entity that is used in the process.

Serialization entities that do require an additional context have RequestAwareEntity concern mixed in. This piece of the code exposes a method called request in every serialization entity that is instantiated during serialization.

An object returned by this method is an instance of EntityRequest, which behaves like an OpenStruct object, with the difference that it will raise an error if an unknown method is called.

In other words, in the previous example, request method will return an instance of EntityRequest that responds to current_user method. It will be available in every serialization entity instantiated by MyResourceSerializer.

EntityRequest is a workaround for #20045 and is meant to be refactored soon. Please avoid passing an additional context that is not required by a serialization entity.

At the moment, the context that is passed to entities most often is current_user and project.

Payload created by a serializer is usually a representation of the backed code, combined with the current request data. Therefore, technically, serializers are presenters that create payload consumed by a frontend code, usually Vue components.

In GitLab, it is possible to use presenters, but BaseSerializer still needs to learn how to use it, see #30898.

It is possible to use presenters when serializer is used to represent only a single object. It is not supported when ActiveRecord::Relation is being serialized.

MyObjectSerializer.new.represent(object.present)

Best practices

  1. Do not invoke a serializer from within a serialization entity.

    If you need to use a serializer from within a serialization entity, it is possible that you are missing a class for an important domain concept.

    Consider creating a new domain class and a corresponding serialization entity for it.

  2. Use only one approach to switch behavior of the serializer.

    It is possible to use a few approaches to switch a behavior of the serializer. Most common are using a Fluent Interface and creating a separate represent_something methods.

    Whatever you choose, it might be better to use only one approach at a time.

  3. Do not forget about creating specs for serialization entities.

    Writing tests for the serializer indeed does cover testing a behavior of serialization entities that the serializer instantiates. However it might be a good idea to write separate tests for entities as well, because these are meant to be reused in different serializers, and a serializer can change a behavior of a serialization entity.

  4. Use ActiveRecord::Relation where possible

    Using an ActiveRecord::Relation might help from the performance perspective.

  5. Be diligent about passing an additional context from the controller.

    Using EntityRequest and RequestAwareEntity is a workaround for the lack of high-level mechanism. It is meant to be refactored, and current implementation is error prone. Imagine the situation that one serialization entity requires request.user attribute, but the second one wants request.current_user. When it happens that these two entities are used in the same serialization request, you might need to pass both parameters to the serializer, which is not a perfect situation.

    When in doubt, pass only current_user and project if these are required.

  6. Keep performance concerns in mind

    Using a serializer incorrectly can have significant impact on the performance.

    Because serializers are technically presenters, it is often necessary to calculate, for example, paths to various controller-actions. Since using URL helpers usually involve passing project and namespace adding includes(project: :namespace) in the serializer, can help to avoid N+1 queries.

    Also, try to avoid using Enumerable#map or other methods that will execute a database query eagerly.

  7. Avoid passing only and except from the controller.

  8. Write tests checking for N+1 queries.

  9. Write controller tests for actions / formats using serializers.

  10. Write tests that check if only necessary data is exposed.

  11. Write tests that check if no sensitive data is exposed.

Future