Skip to content

Commit

Permalink
Merge pull request #319 from ahx/global-json-schemer-config
Browse files Browse the repository at this point in the history
Add support for custom JSONSchemer configuration
  • Loading branch information
ahx authored Jan 22, 2025
2 parents 140c53f + 81f5100 commit ce2d556
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/openapi_first/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def self.build_router(contents, filepath:, config:)
end

def initialize(contents, filepath:, config:)
@schemer_configuration = JSONSchemer::Configuration.new(
meta_schema: detect_meta_schema(contents, filepath),
insert_property_defaults: true
)
@schemer_configuration = JSONSchemer.configuration.clone
@schemer_configuration.meta_schema = detect_meta_schema(contents, filepath)
@schemer_configuration.insert_property_defaults = true

@config = config
@contents = RefResolver.for(contents, dir: filepath && File.dirname(filepath))
end
Expand Down
42 changes: 42 additions & 0 deletions spec/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,48 @@ def build_request(path, method: 'GET')
expect(validated.operation_id).to eq('showPetById')
end
end

context 'with customized global JSONSchemer configuration' do
before do
JSONSchemer.configure do |config|
config.formats = { 'pete' => ->(instance, _format) { instance == 'pete' } }
end
end

let(:definition) do
OpenapiFirst.parse({
'openapi' => '3.1.0',
'paths' => {
'/' => {
'post' => {
'requestBody' => {
'content' => {
'application/json' => {
'schema' => {
'type' => 'string',
'format' => 'pete'
}
}
}
}
}
}
}
})
end

it 'uses the global configuration' do
request = Rack::Request.new(Rack::MockRequest.env_for('/', method: 'POST', input: '"bob"', 'CONTENT_TYPE' => 'application/json'))
expect(
definition.validate_request(request)
).not_to be_valid

request = Rack::Request.new(Rack::MockRequest.env_for('/', method: 'POST', input: '"pete"', 'CONTENT_TYPE' => 'application/json'))
expect(
definition.validate_request(request, raise_error: true)
).to be_valid
end
end
end

describe '#validate_response' do
Expand Down

0 comments on commit ce2d556

Please sign in to comment.