Skip to main content

Test definitions (OpenAPI schemas)

Overview

OpenAPI component schemas live in DefinitionRegistry, registered from each gem’s lib/decidim/rest_full/<feature>/test/definitions/.

When to use

  • You add a new resource type to responses or request bodies.
  • You tighten property descriptions for ReDoc.

Example

1. Add a definition file

decidim-restfull-widgets/lib/decidim/rest_full/test/definitions/widget.rb

Decidim::RestFull::Core::DefinitionRegistry.register_resource(:widget) do
{
type: :object,
title: "Widget",
properties: {
id: { type: :string },
type: { type: :string, enum: ["widget"] },
attributes: {
type: :object,
properties: {
title: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop) },
created_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:creation_date) },
updated_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:edition_date) }
},
required: [:created_at, :updated_at, :title],
additionalProperties: false
}
},
required: [:id, :type, :attributes]
}
end

2. Choose register_resource or register_object

  • register_resource(:widget) — JSON:API resource body; also registers :widget_index_response and :widget_item_response.
  • register_object(:my_payload) — arbitrary object (errors, job accepted, OAuth grant, …).

3. Use auto-generated response names in RSwag

After register_resource(:widget):

SymbolUse in RSwag
:widgetresource schema
:widget_index_responseGET index
:widget_item_responseGET show / sync body

4. Require files from the barrel

decidim-restfull-widgets/lib/decidim/rest_full/widgets/test_definitions.rb

require_relative "../test/definitions/widget"

5. Register the barrel on the engine

decidim-restfull-widgets/lib/decidim/rest_full/widgets/engine.rb

ext.open_api_definitions(
File.join(Widgets::ENGINE_ROOT, "lib/decidim/rest_full/widgets/test_definitions.rb")
)

Do not edit decidim-restfull-core/lib/decidim/rest_full/test/definitions.rb for feature schemas.

6. Reference schemas in request specs

decidim-restfull-widgets/spec/requests/decidim/api/rest_full/widgets/widgets_controller_show_spec.rb

response "200", "Widget found" do
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:widget_item_response)
run_test!
end

Common $ref symbols (core)

Reuse these in attributes instead of redefining type / format. Loaded from decidim-restfull-core/lib/decidim/rest_full/test/definitions/ (shared.rb + core.rb).

R = Decidim::RestFull::Core::DefinitionRegistry

created_at: { "$ref" => R.reference(:creation_date) }
updated_at: { "$ref" => R.reference(:edition_date) }
published_at: { "$ref" => R.reference(:publication_date) }
title: { "$ref" => R.reference(:translated_prop) }

Dates and i18n

SymbolUse on attributeOpenAPI
:creation_datecreated_atstring, format: date-time
:edition_dateupdated_atstring, format: date-time
:publication_datepublished_atstring, format: date-time
:translated_proptitle, body, description, …locale hash
:localesingle locale codestring
:localeslocales[] query paramarray of :locale
:time_zoneorg time_zonestring (IANA)

Components, spaces, manifests

SymbolUse for
:component_typeJSON:API type on component resources
:generic_componentcomponent list/show item
:space_type, :space_classesspace metadata
:space_manifest, :component_manifest, :resource_manifestmanifest enums

Errors, jobs, auth

SymbolUse for
:error, :error_responseAPI errors (response "4xx")
:rest_full_api_job_accepted202 async body
:rest_full_api_job_detailGET /jobs/:uuid
:rest_full_api_jobs_index_responseGET /jobs index
:client_credential, :oauth_grant_param, …OAuth token bodies

Feature gems (examples)

SymbolGem
:forms_validation_error_response, :forms_locale_metaforms
:draft_proposal, :proposalproposals
:blogblogs

Grep register_object / register_resource under decidim-restfull-*/lib/decidim/rest_full/**/test/definitions/ for the full list.

Link helpers on resource links: DefinitionRegistry#get_action_link, #post_action_link, #resource_link.

Rules

RuleDetail
No hand-edited openapi.jsonRegenerate with bin/openapi-stale / yarn gen:openapi-spec.
DescriptionsSet on resources and non-obvious properties.
Formsquestionnaire, questionnaire_response, submission_request, … in forms gem.
CasePath
ConsumerAny *_spec.rb under spec/requests/ with schema "$ref"

See also