class SoberSwag::Compiler::Path

This compiler transforms a {SoberSwag::Controller::Route} object into its associated OpenAPI V3 definition. These definitions are [called “paths” in the OpenAPI V3 spec](swagger.io/docs/specification/paths-and-operations/), thus the name of this compiler.

It only compiles a single “path” at a time.

Attributes

compiler[R]

@return [SoberSwag::Compiler] the compiler used for type compilation

route[R]

@return [SoberSwag::Controller::Route]

Public Class Methods

new(route, compiler) click to toggle source

@param route [SoberSwag::Controller::Route] a route to use @param compiler [SoberSwag::Compiler] the compiler to use for type compilation

# File lib/sober_swag/compiler/path.rb, line 13
def initialize(route, compiler)
  @route = route
  @compiler = compiler
end

Public Instance Methods

params() click to toggle source

An array of all parameters, be they in the query or in the path. See [this page](swagger.io/docs/specification/serialization/) for what that looks like.

@return [Array<Hash>]

# File lib/sober_swag/compiler/path.rb, line 67
def params
  query_params + path_params
end
path_params() click to toggle source

An array of schemas for all path parameters.

@return [Array<Hash>] the schemas

# File lib/sober_swag/compiler/path.rb, line 87
def path_params
  if route.path_params_class
    compiler.path_params_for(route.path_params_class)
  else
    []
  end
end
query_params() click to toggle source

An array of schemas for all query parameters.

@return [Array<Hash>] the schemas

# File lib/sober_swag/compiler/path.rb, line 75
def query_params
  if route.query_params_class
    compiler.query_params_for(route.query_params_class)
  else
    []
  end
end
request_body() click to toggle source

The schema for a request body. Matches [this spec.](swagger.io/docs/specification/paths-and-operations/)

@return [Hash] the schema

# File lib/sober_swag/compiler/path.rb, line 100
def request_body
  return nil unless route.request_body_class

  {
    required: true,
    content: {
      'application/json': {
        schema: compiler.body_for(route.request_body_class)
      }
    }
  }
end
responses() click to toggle source

An array of “response” objects from swagger.

@return [Hash{String => Hash}]

response code to response object.
# File lib/sober_swag/compiler/path.rb, line 46
def responses # rubocop:disable Metrics/MethodLength
  route.response_serializers.map { |status, serializer|
    [
      status.to_s,
      {
        description: route.response_descriptions[status],
        content: {
          'application/json': {
            schema: compiler.response_for(serializer.type)
          }
        }
      }
    ]
  }.to_h
end
schema() click to toggle source

The OpenAPI V3 “path” object for the associated {SoberSwag::Controller::Route}

@return [Hash] the OpenAPI V3 description

# File lib/sober_swag/compiler/path.rb, line 30
def schema
  base = {}
  base[:summary] = route.summary if route.summary
  base[:description] = route.description if route.description
  base[:parameters] = params if params.any?
  base[:responses] = responses
  base[:requestBody] = request_body if request_body
  base[:tags] = tags if tags
  base
end
tags() click to toggle source

The tags for this path. @return [Array<String>] the tags

# File lib/sober_swag/compiler/path.rb, line 116
def tags
  return nil unless route.tags.any?

  route.tags
end