Class: AnotherApi::OpenAPI::Generator

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

Overview

Orchestrates OpenAPI 3.1 spec generation from ApiSerializer schemas and EndpointMetadata DSL declarations on controllers.

spec = AnotherApi::OpenAPI::Generator.generate
File.write("openapi.json", JSON.pretty_generate(spec))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: AnotherApi::OpenAPI.configuration) ⇒ Generator

Returns a new instance of Generator.



16
17
18
# File 'lib/another_api/openapi/generator.rb', line 16

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

Class Method Details

.generate(configuration: AnotherApi::OpenAPI.configuration) ⇒ Object



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

def self.generate(configuration: AnotherApi::OpenAPI.configuration)
  new(configuration: configuration).generate
end

Instance Method Details

#generateObject



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

def generate
  eager_load_api_controllers!
  schemas = SchemaBuilder.new(schema_registry, configuration: @configuration).build_all
  paths = PathBuilder.new(api_endpoints, known_schemas: schemas.keys, configuration: @configuration).build_all

  spec = {
    openapi: "3.1.0",
    info: build_info,
    paths: paths,
    components: {
      schemas: schemas,
      securitySchemes: @configuration.security_schemes,
      parameters: @configuration.common_parameters
    },
    security: @configuration.security
  }

  servers = @configuration.servers || default_servers
  spec[:servers] = servers if servers && !servers.empty?
  spec
end