Skip to content

Instantly share code, notes, and snippets.

@backus
Last active April 29, 2022 14:49
Show Gist options
  • Save backus/75252df00a4f5574f19e72bebbf0bb71 to your computer and use it in GitHub Desktop.
Save backus/75252df00a4f5574f19e72bebbf0bb71 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'openapi3_parser', '= 0.9.2'
end
require 'pathname'
require 'yaml'
require 'openapi3_parser'
raw = YAML.load_file('openapi.yaml')
puts 'Parsing the YAML works fine:'
doc = Openapi3Parser.load(raw)
puts ' OpenAPI document parsed successfully'
puts
puts 'I can access each component directly and everything is fine:'
doc.components.schemas.keys.each do |key|
puts " #{key} => #{doc.components.schemas[key].inspect}"
end
puts
puts 'If I access a component that uses components/schemas/Null though via a $ref, I get an error:'
puts
doc.paths['/blog/post'].post.responses['201'].content['application/json'].schema
---
openapi: 3.0.3
info:
title: Bug Report
version: v0
paths:
/blog/post:
post:
description: x
responses:
"201":
description: x
content:
application/json:
schema:
$ref: "#/components/schemas/BlogPost"
components:
schemas:
"Null":
not:
anyOf:
- type: string
- type: number
- type: boolean
- type: object
- type: array
description: |-
The literal value `null`.
NOTE: This is a workaround for OpenAPI 3.0's lack of support for nullable ref types. See: https://github.com/OAI/OpenAPI-Specification/issues/1368#issuecomment-580103688
title: "Null"
Description:
type: string
BlogPost:
type: object
properties:
title:
type: string
body:
type: string
description:
oneOf:
- "$ref": "#/components/schemas/Description"
- "$ref": "#/components/schemas/Null"
$ ruby bug.rb
Parsing the YAML works fine:
OpenAPI document parsed successfully
I can access each component directly and everything is fine:
Null => Openapi3Parser::Node::Schema(#/components/schemas/Null)
Description => Openapi3Parser::Node::Schema(#/components/schemas/Description)
BlogPost => Openapi3Parser::Node::Schema(#/components/schemas/BlogPost)
If I access a component that uses components/schemas/Null though via a $ref, I get an error:
/Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node_factory/field.rb:110:in `validate': Invalid data for #/paths/%2Fblog%2Fpost/post/responses/201/content/application%2Fjson/schema/%24ref: #/components/schemas/BlogPost does not resolve to a valid object (Openapi3Parser::Error::InvalidData)
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node_factory/field.rb:86:in `data'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node_factory/field.rb:63:in `data'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node_factory/field.rb:43:in `node'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node_factory/reference.rb:48:in `build_node'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node_factory/object.rb:51:in `node'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node/placeholder.rb:49:in `node'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node/placeholder.rb:12:in `resolve'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node/object.rb:35:in `[]'
from /Users/developer/.rbenv/versions/2.6.9/lib/ruby/gems/2.6.0/gems/openapi3_parser-0.9.2/lib/openapi3_parser/node/media_type.rb:11:in `schema'
from bug.rb:29:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment