-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort HTTP methods, response status codes, and contents lexicographica…
…lly (#163)
- Loading branch information
Showing
6 changed files
with
330 additions
and
303 deletions.
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
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
class << RSpec::OpenAPI::SchemaSorter = Object.new | ||
# Sort some unpredictably ordered properties in a lexicographical manner to make the order more predictable. | ||
# | ||
# @param [Hash|Array] | ||
def deep_sort!(spec) | ||
# paths | ||
deep_sort_by_selector!(spec, 'paths') | ||
|
||
# methods | ||
deep_sort_by_selector!(spec, 'paths.*') | ||
|
||
# response status code | ||
deep_sort_by_selector!(spec, 'paths.*.*.responses') | ||
|
||
# content-type | ||
deep_sort_by_selector!(spec, 'paths.*.*.responses.*.content') | ||
end | ||
|
||
private | ||
|
||
# @param [Hash] base | ||
# @param [String] selector | ||
def deep_sort_by_selector!(base, selector) | ||
RSpec::OpenAPI::HashHelper.matched_paths(base, selector).each do |paths| | ||
deep_sort_hash!(base.dig(*paths)) | ||
end | ||
end | ||
|
||
def deep_sort_hash!(hash) | ||
sorted = hash.entries.sort_by { |k, _| k }.to_h | ||
hash.replace(sorted) | ||
end | ||
end |
Oops, something went wrong.