-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement smarter merge #55
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,22 +25,15 @@ def normalize_keys(spec) | |
# Also this needs to be aware of OpenAPI details unlike an ordinary deep_reverse_merge | ||
# because a Hash-like structure may be an array whose Hash elements have a key name. | ||
# | ||
# TODO: Perform more intelligent merges like rerouting edits / merging types | ||
# TODO: Should we probably force-merge `summary` regardless of manual modifications? | ||
def deep_reverse_merge!(base, spec) | ||
spec.each do |key, value| | ||
if base[key].is_a?(Hash) && value.is_a?(Hash) | ||
deep_reverse_merge!(base[key], value) | ||
elsif !base.key?(key) | ||
base[key] = value | ||
elsif base[key].is_a?(Array) && value.is_a?(Array) | ||
# parameters need to be merged as if `name` and `in` were the Hash keys. | ||
if key == 'parameters' | ||
base[key] |= value | ||
base[key].uniq! { |param| param.slice('name', 'in') } | ||
if !base[key].key?("$ref") | ||
deep_reverse_merge!(base[key], value) | ||
end | ||
else | ||
# no-op | ||
base[key] = value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to https://github.com/k0kubun/rspec-openapi/pull/55/files#r877575816, as long as it updates only fields that rspec-openapi generates, it's probably fine, I guess. |
||
end | ||
end | ||
base | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"openapi": "3.0.3", | ||
"info": { | ||
"title": "My API", | ||
"version": "1.0.0" | ||
}, | ||
"servers": [], | ||
"paths": { | ||
"/tables": { | ||
"get": { | ||
"summary": "index", | ||
"tags": [ | ||
"Table" | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "foo", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/Table" | ||
}, | ||
"example": [ | ||
{ | ||
"id": 1, | ||
"name": "THIS SHOULD BE UPDATED" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"parameters": [] | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"Table": { | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "integer", | ||
"format": "int64" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"openapi": "3.0.3", | ||
"info": { | ||
"title": "My API", | ||
"version": "1.0.0" | ||
}, | ||
"servers": [], | ||
"paths": { | ||
"/tables": { | ||
"get": { | ||
"summary": "index", | ||
"tags": [ | ||
"Table" | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "foo", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/Table" | ||
}, | ||
"example": [ | ||
{ | ||
"id": 9999, | ||
"name": "UPDATED" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"parameters": [] | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"Table": { | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "integer", | ||
"format": "int64" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"paths": { | ||
"/tables": { | ||
"get": { | ||
"summary": "index", | ||
"tags": [ | ||
"Table" | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "foo", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "integer" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"example": [ | ||
{ | ||
"id": 9999, | ||
"name": "UPDATED" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"parameters": [] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
require 'json' | ||
require "rspec/openapi/schema_merger" | ||
|
||
RSpec.describe "SchemaMerger" do | ||
include SpecHelper | ||
|
||
let(:base_path) do | ||
File.expand_path('spec/rspec/schema_merger/base.json', repo_root) | ||
end | ||
|
||
let(:input_path) do | ||
File.expand_path('spec/rspec/schema_merger/input.json', repo_root) | ||
end | ||
|
||
let(:expected_path) do | ||
File.expand_path('spec/rspec/schema_merger/expected.json', repo_root) | ||
end | ||
|
||
it "overwrite the supported key, but leaves the unsupported keys" do | ||
base_json = JSON.load(File.read(base_path)) | ||
input_json = JSON.load(File.read(input_path)) | ||
res = RSpec::OpenAPI::SchemaMerger.reverse_merge!(base_json, input_json) | ||
expected_json = JSON.load(File.read(expected_path)) | ||
expect(res).to eq(expected_json) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a special treatment to avoid merging anything to Reference Object.
The reference object can have only
$ref
field.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this for the case that
$ref
is manually added to an OpenAPI schema file? If you manually maintain the schema of the response, why don't you skip updating OpenAPI from such test cases? Do you have a test case that calls multiple endpoints and want to update OpenAPI for only one of these?