Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* `TypeNullIn30`: detect `type: "null"` usage in 3.0 documents (3.1 primitive)
* `WebhooksIn30`: detect root-level `webhooks` usage in 3.0 documents (3.1 addition)
* `ConstIn30`: detect `const` usage in 3.0 documents (3.1 addition)
* `DynamicRefIn30`: detect `$dynamicRef` usage in 3.0 documents (3.1 addition)
* support 3.1-style numeric `exclusiveMinimum` / `exclusiveMaximum` in value validation (standalone bound, not a Boolean modifier on `minimum` / `maximum`)
* support `type: "null"` (3.1 primitive) in value validation
* support root-level `webhooks` (OpenAPI 3.1) in the parse layer
Expand Down
2 changes: 2 additions & 0 deletions lib/openapi_parser/spec_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative 'spec_validator/rules/type_null_in_30'
require_relative 'spec_validator/rules/webhooks_in_30'
require_relative 'spec_validator/rules/const_in_30'
require_relative 'spec_validator/rules/dynamic_ref_in_30'

module OpenAPIParser
class SpecViolationError < OpenAPIError
Expand Down Expand Up @@ -67,6 +68,7 @@ def rules
Rules::TypeNullIn30,
Rules::WebhooksIn30,
Rules::ConstIn30,
Rules::DynamicRefIn30,
]
end
end
Expand Down
25 changes: 25 additions & 0 deletions lib/openapi_parser/spec_validator/rules/dynamic_ref_in_30.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module OpenAPIParser
class SpecValidator
module Rules
# `$dynamicRef` is a JSON Schema 2020-12 referencing mechanism that
# 3.1 adopted. 3.0 does not recognize it. The parse layer keeps the
# value as raw schema data; this rule reports the version mismatch.
class DynamicRefIn30 < Rule
def check(root)
return [] unless version == :v3_0

violations = []
each_schema(root) do |schema|
next unless schema.raw_schema.is_a?(Hash) && schema.raw_schema.key?('$dynamicRef')

violations << violation(
path: schema.object_reference,
message: '`$dynamicRef` is a 3.1 addition (from JSON Schema 2020-12); 3.0 only knows `$ref`',
)
end
violations
end
end
end
end
end
4 changes: 4 additions & 0 deletions sig/openapi_parser/spec_validator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ module OpenAPIParser
class ConstIn30 < Rule
def check: (OpenAPIParser::Schemas::OpenAPI root) -> Array[SpecValidator::SpecViolation]
end

class DynamicRefIn30 < Rule
def check: (OpenAPIParser::Schemas::OpenAPI root) -> Array[SpecValidator::SpecViolation]
end
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/data/openapi_3_1/dynamic_ref_30.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.3
info:
title: Tree API
version: '1.0'
paths:
/trees:
get:
summary: Fetch a tree
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Node'
components:
schemas:
Node:
type: object
properties:
children:
type: array
# `$dynamicRef` is a JSON Schema 2020-12 dynamic referencing keyword
# adopted by 3.1; 3.0 only knows `$ref`, so this is a spec violation.
items:
$dynamicRef: '#node'
26 changes: 26 additions & 0 deletions spec/data/openapi_3_1/dynamic_ref_31.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.1.0
info:
title: Tree API
version: '1.0'
paths:
/trees:
get:
summary: Fetch a tree
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Node'
components:
schemas:
Node:
type: object
properties:
children:
type: array
# `$dynamicRef` is legitimate under 3.1 (JSON Schema 2020-12), so no
# violation is expected here.
items:
$dynamicRef: '#node'
14 changes: 14 additions & 0 deletions spec/openapi_parser/spec_validator/integration_3_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,18 @@ def expect_clean(file)
expect_clean('const_31.yaml')
end
end

describe '$dynamicRef (JSON Schema 2020-12 referencing new in 3.1)' do
it 'warns on the version-mismatched document under :warn' do
expect_mismatch_warns('dynamic_ref_30.yaml', [:dynamic_ref_in30])
end

it 'raises SpecViolationError on the version-mismatched document under :raise' do
expect_mismatch_raises('dynamic_ref_30.yaml', [:dynamic_ref_in30])
end

it 'stays clean on the correctly-versioned document' do
expect_clean('dynamic_ref_31.yaml')
end
end
end
64 changes: 64 additions & 0 deletions spec/openapi_parser/spec_validator/rules/dynamic_ref_in_30_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require_relative '../../../spec_helper'

RSpec.describe 'OpenAPIParser::SpecValidator::Rules::DynamicRefIn30' do
def base_doc(openapi_version_string, sample_schema)
{
'openapi' => openapi_version_string,
'info' => { 'title' => 'test', 'version' => '1.0' },
'paths' => {},
'components' => { 'schemas' => { 'Sample' => sample_schema } },
}
end

def doc_with_dynamic_ref(openapi_version_string)
raw = base_doc(openapi_version_string, { '$dynamicRef' => '#meta' })
OpenAPIParser.parse(raw, strict_reference_validation: false)
end

def doc_without_dynamic_ref(openapi_version_string)
raw = base_doc(openapi_version_string, { 'type' => 'string' })
OpenAPIParser.parse(raw, strict_reference_validation: false)
end

def run_rule_for(root)
OpenAPIParser::SpecValidator::Rules::DynamicRefIn30.new(root.openapi_version).check(root)
end

context 'with a 3.1 document using $dynamicRef' do
it 'reports no violation' do
root = doc_with_dynamic_ref('3.1.0')
expect(run_rule_for(root)).to eq []
end
end

context 'with a 3.1 document without $dynamicRef' do
it 'reports no violation' do
root = doc_without_dynamic_ref('3.1.0')
expect(run_rule_for(root)).to eq []
end
end

context 'with a 3.0 document using $dynamicRef' do
it 'reports one violation pointing at the offending schema' do
root = doc_with_dynamic_ref('3.0.0')
violations = run_rule_for(root)
expect(violations.size).to eq 1
expect(violations.first.path).to eq '#/components/schemas/Sample'
expect(violations.first.rule_name).to eq :dynamic_ref_in30
end
end

context 'with a 3.0 document without $dynamicRef' do
it 'reports no violation' do
root = doc_without_dynamic_ref('3.0.0')
expect(run_rule_for(root)).to eq []
end
end

context 'with an :unknown version document' do
it 'reports no violation (rule skipped)' do
root = doc_with_dynamic_ref('4.0.0')
expect(run_rule_for(root)).to eq []
end
end
end