Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,25 @@ Not yet supported:
- `patternProperties`
- `$ref` siblings

### OpenAPI 3.2

OpenAPI 3.2 documents also use the OpenAPI 3 driver.

Supported 3.2 additions:

- the `query` operation (`QUERY` requests are validated like `POST`, including the request body and `Content-Type`)
- `additionalOperations` (custom methods are matched by exact case, so `PURGE` and `purge` are different operations)
- `components.mediaTypes` `$ref`s in request and response `content`
- Discriminator `defaultMapping`

These apply only to documents that declare 3.2. Pass `allow_3_2_features: true` in `parser_options` to apply them to earlier documents. `strict_specification_version` (see above) also flags 3.2 additions in earlier documents.

Parsed but not validated:

- `itemSchema` (streaming response bodies such as `text/event-stream`)
- parameters with `in: querystring`
- `$self` (not used as the base URI for `$ref`s)

### Upgrading from Committee 2.* to 3.*

Committee 3.* has many breaking changes so we recommend upgrading to the latest release on 2.* and fixing any deprecation errors you see before upgrading to 3.*. The steps would be roughly as follows:
Expand Down
2 changes: 1 addition & 1 deletion committee.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_dependency "json_schema", "~> 0.14", ">= 0.14.3"

s.add_dependency "rack", ">= 1.5"
s.add_dependency "openapi_parser", "~> 2.4"
s.add_dependency "openapi_parser", "~> 2.8"

s.add_development_dependency "minitest", "~> 5.3"
s.add_development_dependency "rack-test"
Expand Down
4 changes: 2 additions & 2 deletions lib/committee/drivers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def self.load_from_file(schema_path, parser_options: {})
# @return [Committee::Driver]
def self.load_from_data(hash, schema_path = nil, parser_options: {})
if (version = hash['openapi'])
# release drops prerelease tags, so 3.2.0-rc1 counts as 3.2
# release drops prerelease tags, so 3.3.0-rc1 counts as 3.3
version = Gem::Version.new(version).release
raise OpenAPI3Unsupported.new('Committee does not support OpenAPI 3.2+ yet') if version >= Gem::Version.new("3.2")
raise OpenAPI3Unsupported.new('Committee does not support OpenAPI 3.3+ yet') if version >= Gem::Version.new("3.3")
end

if version && version >= Gem::Version.new("3.0")
Expand Down
4 changes: 4 additions & 0 deletions lib/committee/drivers/open_api_3/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def build_router(options)

# OpenAPI3 only
def operation_object(path, method)
# 3.2 lookups keep the case to tell additionalOperations apart (`Get` vs `GET`);
# earlier documents only have lowercase method fields
method = method.to_s
method = method.downcase unless open_api.use_3_2_features?
request_operation = open_api.request_operation(method, path)
return nil unless request_operation

Expand Down
34 changes: 25 additions & 9 deletions lib/committee/schema_validator/open_api_3/operation_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def original_path
request_operation.original_path
end

# lowercase for standard methods; additionalOperations keep their case
def http_method
request_operation.http_method
method = request_operation.http_method
custom_operation? ? method : method.downcase
end

def coerce_path_parameter(validator_option, path_params = nil)
Expand Down Expand Up @@ -66,14 +68,24 @@ def validate_response_params(status_code, headers, response_data, strict, check_
end

def validate_request_params(_path_params, query_params, body_params, headers, validator_option)
case request_operation.http_method
when 'get', 'delete', 'head'
validate_get_request_params(query_params, headers, validator_option)
when 'post', 'put', 'patch', 'options'
validate_post_request_params(query_params, body_params, headers, validator_option)
else
raise "Committee OpenAPI3 not support #{request_operation.http_method} method"
end
if body_method?
validate_post_request_params(query_params, body_params, headers, validator_option)
else
validate_get_request_params(query_params, headers, validator_option)
end
end

# query (3.2) carries a body like post. trace never does (RFC 9110 §9.3.8), so its
# requestBody is ignored. 3.2 additionalOperations do when they define a requestBody
def body_method?
case http_method
when 'get', 'delete', 'head', 'trace'
false
when 'post', 'put', 'patch', 'options', 'query'
true
else
!request_operation.operation_object&.request_body.nil?
Comment thread
modosc marked this conversation as resolved.
Resolved
end
end

def optional_body?
Expand Down Expand Up @@ -107,6 +119,10 @@ def query_parameter_names

private

def custom_operation?
request_operation.path_item&.additional_operations&.value?(request_operation.operation_object) || false
end

# @return [OpenAPIParser::SchemaValidator::Options]
def build_openapi_parser_body_option(validator_option)
build_openapi_parser_option(validator_option, validator_option.coerce_form_params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def call(request, path_params, query_params, body_params, headers)
private

def check_content_type(request, content_type)
# support post, put, patch, options only
return true unless request.post? || request.put? || request.patch? || request.options?
return true unless @operation_object.body_method?
return true if @operation_object.valid_request_content_type?(content_type)
return true if @operation_object.optional_body? && empty_request?(request)

Expand Down
4 changes: 1 addition & 3 deletions lib/committee/schema_validator/open_api_3/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def operation_object(request)
path = request.path
path = path.gsub(@prefix_regexp, '') if @prefix_regexp

request_method = request.request_method.downcase

@schema.operation_object(path, request_method)
@schema.operation_object(path, request.request_method)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/committee/test/except_parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def resolve_operation
path = path.gsub(Regexp.new("\\A#{Regexp.escape(prefix)}"), '')
end

schema.operation_object(path, @request.request_method.downcase)
schema.operation_object(path, @request.request_method)
end

# Find the OpenAPI3 schema object for a parameter by name and location.
Expand Down
13 changes: 11 additions & 2 deletions lib/committee/test/schema_coverage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def initialize(schema)
end

def update_response_coverage!(path, method, response_status)
method = method.to_s.downcase
method = coverage_method(path, method)
response_status = response_status.to_s

@covered[path] ||= {}
Expand All @@ -63,7 +63,8 @@ def report
path_item._openapi_all_child_objects.each do |object_name, object|
next unless object.is_a?(OpenAPIParser::Schemas::Operation)

method = object_name.split('/').last&.downcase
# JSON Pointer segment (RFC 6901), so `SYNC~V2` appears as `SYNC~0V2`
method = object_name.split('/').last&.gsub('~1', '/')&.gsub('~0', '~')
next unless method

report[path_name][method] ||= {}
Expand All @@ -90,6 +91,14 @@ def report
def report_flatten
self.class.flatten_report(report)
end

private

# an exact additionalOperations key keeps its case; anything else is a lowercase standard method
def coverage_method(path, method)
method = method.to_s
schema.open_api.paths.path[path]&.additional_operations&.key?(method) ? method : method.downcase
end
end
end
end
199 changes: 199 additions & 0 deletions test/data/openapi3/3_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
openapi: 3.2.0
info:
version: 1.0.0
title: OpenAPI 3.2 Test
description: A Sample file
paths:
/items:
query:
description: search items with a request body
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ItemQuery'
responses:
'200':
summary: matching items
description: success
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Item'
additionalOperations:
PURGE:
description: purge cached items
parameters:
- name: older_than
in: query
required: false
schema:
type: integer
responses:
'204':
description: purged
purge:
description: a distinct operation, since method names are case-sensitive
parameters:
- name: scope
in: query
required: true
schema:
type: string
responses:
'204':
description: purged
SEARCH:
description: search items with a custom method
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ItemQuery'
responses:
'200':
description: success
trace:
description: a requestBody that must be ignored, since TRACE carries no content
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ItemQuery'
responses:
'200':
description: success
/pets:
post:
description: create a pet
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'200':
description: success
/shared:
post:
description: request and response bodies from components.mediaTypes
requestBody:
content:
application/json:
$ref: '#/components/mediaTypes/ItemJson'
responses:
'200':
description: success
content:
application/json:
$ref: '#/components/mediaTypes/ItemJson'
/versions:
get:
description: the standard GET
parameters:
- name: standard
in: query
required: true
schema:
type: string
responses:
'200':
description: success
additionalOperations:
Get:
description: a distinct method from GET, since method names are case-sensitive
parameters:
- name: custom
in: query
required: true
schema:
type: string
responses:
'200':
description: success
SYNC~V2:
description: a method name with a character escaped in JSON Pointers
responses:
'200':
description: success
/filter:
get:
description: whole query string as one parameter
parameters:
- name: filter
in: querystring
required: true
content:
application/x-www-form-urlencoded:
schema:
type: object
required:
- q
responses:
'200':
description: success
/events:
get:
description: server-sent events
responses:
'200':
description: event stream
content:
text/event-stream:
itemSchema:
type: object
required:
- data
components:
mediaTypes:
ItemJson:
schema:
$ref: '#/components/schemas/Item'
schemas:
Item:
type: object
required:
- name
properties:
name:
type: string
ItemQuery:
type: object
required:
- name
properties:
name:
type: string
limit:
type: integer
minimum: 1
Pet:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: pet_type
defaultMapping: Cat
Cat:
type: object
required:
- name
properties:
pet_type:
type: string
name:
type: string
Dog:
type: object
required:
- bark
properties:
pet_type:
type: string
bark:
type: boolean
Loading
Loading