-
Notifications
You must be signed in to change notification settings - Fork 77
Much smarter merge: cleanup items that only exist in a previous result #57
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
Changes from all commits
789d9bd
9aaa102
8026e62
e2bc5c5
3a9620d
61b10c9
d0c8c8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # For Ruby 3.0+ | ||
| require 'set' | ||
|
|
||
| class << RSpec::OpenAPI::SchemaCleaner = Object.new | ||
| # Cleanup specific elements that exists in the base but not in the spec | ||
| # | ||
| # @param [Hash] base | ||
| # @param [Hash] spec | ||
| def cleanup!(base, spec) | ||
| # cleanup URLs | ||
| cleanup_hash!(base, spec, 'paths.*') | ||
|
|
||
| # cleanup HTTP methods | ||
| cleanup_hash!(base, spec, 'paths.*.*') | ||
|
|
||
| # cleanup parameters | ||
| cleanup_array!(base, spec, 'paths.*.*.parameters', %w[name in]) | ||
|
|
||
| # cleanup requestBody | ||
| cleanup_hash!(base, spec, 'paths.*.*.requestBody.content.application/json.schema.properties.*') | ||
| cleanup_hash!(base, spec, 'paths.*.*.requestBody.content.application/json.example.*') | ||
|
|
||
| # cleanup responses | ||
| cleanup_hash!(base, spec, 'paths.*.*.responses.*.content.application/json.schema.properties.*') | ||
| cleanup_hash!(base, spec, 'paths.*.*.responses.*.content.application/json.example.*') | ||
| base | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def paths_to_all_fields(obj) | ||
| case obj | ||
| when Hash | ||
| obj.each.flat_map do |k,v| | ||
| k = k.to_s | ||
| [[k]] + paths_to_all_fields(v).map { |x| [k, *x] } | ||
| end | ||
| else | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| def matched_paths(obj, selector) | ||
| selector_parts = selector.split('.').map(&:to_s) | ||
| selectors = paths_to_all_fields(obj).select do |key_parts| | ||
| key_parts.size == selector_parts.size && key_parts.zip(selector_parts).all? do |kp, sp| | ||
| kp == sp || (sp == '*' && kp != nil) | ||
| end | ||
| end | ||
| selectors | ||
| end | ||
|
|
||
| def cleanup_array!(base, spec, selector, fields_for_identity = []) | ||
| marshal = lambda do |obj| | ||
| Marshal.dump(slice(obj, fields_for_identity)) | ||
| end | ||
|
|
||
| matched_paths(base, selector).each do |paths| | ||
| target_array = base.dig(*paths) | ||
| spec_array = spec.dig(*paths) | ||
| unless target_array.is_a?(Array) && spec_array.is_a?(Array) | ||
| next | ||
| end | ||
| spec_identities = Set.new(spec_array.map(&marshal)) | ||
| target_array.select! { |e| spec_identities.include?(marshal.call(e)) } | ||
| target_array | ||
| .sort_by! { |param| [param['__marker'], *fields_for_identity.map {|f| param[f] }].join('-') } | ||
| .each { |param| param.delete('__marker') } | ||
| # Keep the last duplicate with largest __marker, to produce the result stably | ||
| deduplicated = (target_array.reverse.uniq do |param| | ||
| slice(param, fields_for_identity) | ||
| end).reverse | ||
| target_array.replace(deduplicated) | ||
| end | ||
| base | ||
| end | ||
|
|
||
| def cleanup_hash!(base, spec, selector) | ||
| matched_paths(base, selector).each do |paths| | ||
| exist_in_base = !base.dig(*paths).nil? | ||
| not_in_spec = spec.dig(*paths).nil? | ||
| if exist_in_base && not_in_spec | ||
| if paths.size == 1 | ||
| base.delete(paths.last) | ||
| else | ||
| parent_node = base.dig(*paths[0..-2]) | ||
| parent_node.delete(paths.last) | ||
| end | ||
| end | ||
| end | ||
| base | ||
| end | ||
|
|
||
| def slice(obj, fields_for_identity) | ||
| if fields_for_identity.any? | ||
| obj.slice(*fields_for_identity) | ||
| else | ||
| obj | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,40 +20,22 @@ paths: | |
| tags: | ||
| - Table | ||
| parameters: | ||
| - name: page | ||
| in: query | ||
| schema: | ||
| type: integer | ||
| example: 42 | ||
| - name: per | ||
| in: query | ||
| schema: | ||
| type: integer | ||
| example: 10 | ||
| - name: X-Authorization-Token | ||
| in: header | ||
| required: true | ||
| schema: | ||
| type: string | ||
| example: token | ||
| - name: filter[name] | ||
| - name: page | ||
| in: query | ||
| schema: | ||
| type: object | ||
| properties: | ||
| name: | ||
| type: string | ||
| example: | ||
| name: Example Table | ||
| - name: filter[price] | ||
| type: integer | ||
| example: 42 | ||
| - name: per | ||
| in: query | ||
| schema: | ||
| type: object | ||
| properties: | ||
| price: | ||
| type: string | ||
| example: | ||
| price: '0' | ||
| type: integer | ||
| example: 10 | ||
| responses: | ||
| '200': | ||
| description: with flat query parameters | ||
|
|
@@ -83,49 +65,8 @@ paths: | |
| properties: | ||
| message: | ||
| type: string | ||
| no_such_field: | ||
| type: string | ||
| example: This field does not exist in rspec | ||
|
Comment on lines
-86
to
-88
Owner
Author
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. Note) |
||
| example: | ||
| message: Unauthorized | ||
| post: | ||
|
Owner
Author
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. Note) |
||
| summary: create | ||
| tags: | ||
| - Table | ||
| requestBody: | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| properties: | ||
| name: | ||
| type: string | ||
| description: | ||
| type: string | ||
| database_id: | ||
| type: integer | ||
| example: | ||
| name: k0kubun | ||
| description: description | ||
| database_id: 2 | ||
| responses: | ||
| '201': | ||
| description: returns a table | ||
| content: | ||
| application/json: | ||
| schema: | ||
| "$ref": "#/components/schemas/Table" | ||
| example: | ||
| id: 1 | ||
| name: access | ||
| description: logs | ||
| database: | ||
| id: 2 | ||
| name: production | ||
| null_sample: | ||
| storage_size: 12.3 | ||
| created_at: '2020-07-17T00:00:00+00:00' | ||
| updated_at: '2020-07-17T00:00:00+00:00' | ||
| "/tables/{id}": | ||
| get: | ||
| summary: show | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,19 @@ info: | |
| servers: | ||
| - url: http://localhost:3000 | ||
| paths: | ||
| "/no-such-path": | ||
| get: | ||
| summary: no such api | ||
| parameters: [] | ||
| responses: | ||
| '200': | ||
| description: dummy | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: number | ||
| example: | ||
| - 1 | ||
|
Comment on lines
+17
to
+29
Owner
Author
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. Note) |
||
| "/tables": | ||
| get: | ||
| summary: index | ||
|
|
@@ -104,10 +117,13 @@ paths: | |
| type: string | ||
| database_id: | ||
| type: integer | ||
| no_such_field_request: | ||
| type: boolean | ||
|
Comment on lines
+120
to
+121
Owner
Author
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. Note) This is added but does not appear in the |
||
| example: | ||
| name: k0kubun | ||
| description: description | ||
| database_id: 2 | ||
| no_such_field_request: true | ||
|
Owner
Author
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. Note) This is added but does not appear in the |
||
| responses: | ||
| '201': | ||
| description: returns a table | ||
|
|
@@ -164,6 +180,8 @@ paths: | |
| storage_size: | ||
| type: number | ||
| format: float | ||
| no_such_field_response: | ||
| type: boolean | ||
|
Comment on lines
+183
to
+184
Owner
Author
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. Note) This is added but does not appear in the |
||
| created_at: | ||
| type: string | ||
| updated_at: | ||
|
|
@@ -172,6 +190,7 @@ paths: | |
| id: 1 | ||
| name: access | ||
| description: logs | ||
| no_such_field_response: true | ||
|
Owner
Author
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. Note) This is added but does not appear in the |
||
| database: | ||
| id: 2 | ||
| name: production | ||
|
|
||
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.
Note)
filter[name]was removed because it matched the pattern3. paths.*.*.parameters