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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

Fixed: Loading a document no longer raises `NoMethodError: undefined method 'schema' for nil` when a Media Type Object has no `schema` (e.g. it only declares an `example`). `schema` is optional in a Media Type Object; such media types now impose no body-schema constraint.

## 3.4.2

Fixed: Parsing of JSON-formatted query params [issue #476](https://github.com/ahx/openapi_first/issues/476) (thanks @Drowze)
Expand Down
4 changes: 2 additions & 2 deletions lib/openapi_first/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def build_requests(path:, request_method:, operation_object:, parameters:) # rub
end
required_body = operation_object['requestBody']&.resolved&.fetch('required', false) == true
content_objects.map do |content_type, content_object|
content_schema = content_object['schema'].schema(
content_schema = content_object['schema']&.schema(
configuration: schemer_configuration,
after_property_validation: config.after_request_body_property_validation
)
Expand Down Expand Up @@ -171,7 +171,7 @@ def build_responses(responses:, request:)
responses.flat_map do |status, response_object|
headers = build_response_headers(response_object['headers'])
response_object['content']&.map do |content_type, content_object|
content_schema = content_object['schema'].schema(
content_schema = content_object['schema']&.schema(
configuration: schemer_configuration,
after_property_validation: config.after_response_body_property_validation
)
Expand Down
17 changes: 17 additions & 0 deletions spec/data/media-type-without-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.0
info:
title: Media type without schema
version: '1.0'
paths:
/thing:
post:
requestBody:
content:
application/json:
example: '{"name":"foo"}' # valid: media type with example, no schema
responses:
'422':
description: Unprocessable Entity
content:
application/json:
example: '{"errors":["bad"]}' # valid: media type with example, no schema
5 changes: 5 additions & 0 deletions spec/openapi_first_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
expect(definition.paths).to include('/roles')
end

it 'works with a media type that has no schema' do
definition = OpenapiFirst.load('./spec/data/media-type-without-schema.yaml')
expect(definition.paths).to include('/thing')
end

it 'works with YAML' do
definition = OpenapiFirst.load('./spec/data/petstore.yaml')
expect(definition.paths).to include('/pets')
Expand Down
Loading