Skip to content

Commit

Permalink
Support referenced components in "items"
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Nov 8, 2022
1 parent 554b9bb commit d6d875b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/rspec/openapi/components_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ def update!(base, fresh)
generated_schema_names = fresh_schemas.keys
nested_refs = find_non_top_level_nested_refs(base, generated_schema_names)
nested_refs.each do |paths|
parent_name = paths[-4]
property_name = paths[-2]
nested_schema = fresh_schemas.dig(parent_name, 'properties', property_name)
# Slice between the parent name and the element before "$ref"
# [..., "Table", "properties", "database", "$ref"]
# ^idx-1 ^idx ^size-idx
# [..., "Table", "properties", "columns", "items", "$ref"]
# ^idx-1 ^idx ^size-idx
idx_properties = paths.size - 1 - paths.reverse.find_index('properties')
needle = paths.slice(idx_properties - 1, paths.size - idx_properties)
nested_schema = fresh_schemas.dig(*needle)

# Skip if the property using $ref is not found in the parent schema. The property may be removed.
next if nested_schema.nil?
Expand Down Expand Up @@ -53,8 +58,10 @@ def paths_to_top_level_refs(base)
end

def find_non_top_level_nested_refs(base, generated_names)
nested_refs = RSpec::OpenAPI::HashHelper::matched_paths(base, 'components.schemas.*.properties.*.$ref')

nested_refs = [
*RSpec::OpenAPI::HashHelper.matched_paths(base, 'components.schemas.*.properties.*.$ref'),
*RSpec::OpenAPI::HashHelper.matched_paths(base, 'components.schemas.*.properties.*.*.$ref')
]
# Reject already-generated schemas to reduce unnecessary loop
nested_refs.reject do |paths|
ref_link = base.dig(*paths)
Expand Down

0 comments on commit d6d875b

Please sign in to comment.