Class: AnotherApi::OpenAPI::SchemaBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/another_api/openapi/schema_builder.rb

Overview

Walks ApiSerializer variants and produces OpenAPI schema objects.

builder = SchemaBuilder.new(schema_registry)
schemas = builder.build_all  # => {"UserFull" => {...}, ...}

Instance Method Summary collapse

Constructor Details

#initialize(schema_registry, configuration: AnotherApi::OpenAPI.configuration) ⇒ SchemaBuilder

Returns a new instance of SchemaBuilder.



11
12
13
14
# File 'lib/another_api/openapi/schema_builder.rb', line 11

def initialize(schema_registry, configuration: AnotherApi::OpenAPI.configuration)
  @schema_registry = schema_registry
  @configuration = configuration
end

Instance Method Details

#build_allObject

Returns => schema_hash for ALL variants of ALL schemas, plus PaginationMetadata and FilterExpression common schemas.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/another_api/openapi/schema_builder.rb', line 18

def build_all
  schemas = {}

  @schema_registry.each do |schema_name, schema_class|
    store = schema_class.schema_variants_store
    next unless store

    concrete_variants = store.variants.reject(&:abstract?)

    concrete_variants.select { |v| v.type == :serializer }.each do |variant|
      openapi_name = openapi_schema_name(schema_name, variant.name)
      schemas[openapi_name] = build_schema_from_variant(variant, variant.name)
    end

    concrete_variants.select { |v| v.type == :deserializer }.each do |variant|
      openapi_name = "#{schema_name}#{variant.name.to_s.camelize}Input"
      schemas[openapi_name] = build_schema_from_variant(variant)
    end
  end

  schemas["PaginationMetadata"] = @configuration.
  schemas["FilterExpression"] = @configuration.filter_expression_schema

  schemas
end