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

Inherits:
Object
  • 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

Constructor Details

#initialize(link_data) ⇒ ParameterSchemaBuilder

Returns a new instance of ParameterSchemaBuilder.



93
94
95
# File 'lib/committee/drivers/open_api_2.rb', line 93

def initialize(link_data)
  self.link_data = link_data
end

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/committee/drivers/open_api_2.rb', line 100

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 = []

      link_data["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