Skip to content

Refactor schema definitions

Introduce a new DSL through a registry for schemas definitions. This will allow other gems to add definitions to the schemas, and be able to decouple swagger helper from definition files.

Register an object that can get referenced through "#/components/schema/link_object"

Api::RestFull::Definitions.register_object(:link_object) do 
  {
    title: "Link object",
    type: :object,
    properties: {
      title: {type: :string, description: "link title"},
      href: {type: :string, description: "url"}
    }
  }
end

Get a reference

Api::RestFull::Definitions.reference(:link_object) # get openapi 3 ref

Link helpers will be available globaly

Api::RestFull::Definitions.link_resource
Api::RestFull::Definitions.link_post_action
Api::RestFull::Definitions.link_get_action

Introducing also relashionship helper

Api::RestFull::Definitions.has_many("Resources", :blog_post)
Api::RestFull::Definitions.belongs_to("Component", :component, nullable: true)

register a resource, with associate request and response

Api::RestFull::Definitions.register_resource(:blog_post) do 
  {
    title: "A blog post",
    type: :object,
    properties: {
      data: {
        type: :object,
        properties: {title: {type: :string, description: "link title"}},
        required: [:title]
      },
      relationships: {},
      links: {
        type: :object,
        properties: {
          author: Api::RestFull::Definitions.link_resource("Author details", nullable: true),
          create: Api::RestFull::Definitions.link_post_action("Create a post", nullable: true),
          draft: Api::RestFull::Definitions.link_get_action("Current post draft", nullable: true)    
        },
        required: [:properties]
      }
    }
  }
end
Edited by Hadrien Froger