From d6d875bed9efb70e5ae6f339bd470da538bb22a1 Mon Sep 17 00:00:00 2001 From: TATSUNO Yasuhiro Date: Tue, 8 Nov 2022 16:03:11 +0900 Subject: [PATCH] Support referenced components in "items" --- lib/rspec/openapi/components_updater.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/rspec/openapi/components_updater.rb b/lib/rspec/openapi/components_updater.rb index 8839d6b1..b53b63d7 100644 --- a/lib/rspec/openapi/components_updater.rb +++ b/lib/rspec/openapi/components_updater.rb @@ -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? @@ -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)