Class: Committee::Drivers::OpenAPI2::ParameterSchemaBuilder

Inherits:
SchemaBuilder
  • Object
show all
Defined in:
lib/committee/drivers/open_api_2.rb

Overview

ParameterSchemaBuilder converts OpenAPI 2 link parameters, which are not quite JSON schemas (but will be in OpenAPI 3) into synthetic schemas that we can use to do some basic request validation.

Instance Method Summary collapse

Methods inherited from SchemaBuilder

#initialize

Constructor Details

This class inherits a constructor from Committee::Drivers::OpenAPI2::SchemaBuilder

Instance Method Details

#callObject

Returns a tuple of (schema, schema_data) where only one of the two values is present. This is either a full schema that’s ready to go or a hash of unparsed schema data.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/committee/drivers/open_api_2.rb', line 147

def call
  if link_data["parameters"]
    body_param = link_data["parameters"].detect { |p| p["in"] == "body" }
    if body_param
      check_required_fields!(body_param)

      if link_data["parameters"].detect { |p| p["in"] == "form" } != nil
        raise ArgumentError, "Committee: can't mix body parameter " \
          "with form parameters."
      end

      schema_data = body_param["schema"]
      [nil, schema_data]
    else
      link_schema = JsonSchema::Schema.new
      link_schema.properties = {}
      link_schema.required = []

      parameters = link_data["parameters"].reject { |param_data| param_data["in"] == "header" }
      parameters.each do |param_data|
        check_required_fields!(param_data)

        param_schema = JsonSchema::Schema.new

        # We could probably use more validation here, but the formats of
        # OpenAPI 2 are based off of what's available in JSON schema, and
        # therefore this should map over quite well.
        param_schema.type = [param_data["type"]]

        # And same idea: despite parameters not being schemas, the items
        # key (if preset) is actually a schema that defines each item of an
        # array type, so we can just reflect that directly onto our
        # artifical schema.
        if param_data["type"] == "array" && param_data["items"]
          param_schema.items = param_data["items"]
        end

        link_schema.properties[param_data["name"]] = param_schema
        if param_data["required"] == true
          link_schema.required << param_data["name"]
        end
      end

      [link_schema, nil]
    end
  end
end