Skip to content
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

$ref enhancement: Auto-generate components referenced in "items" #80

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 23 additions & 1 deletion spec/rails/app/controllers/tables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class TablesController < ApplicationController

def index
response.set_header('X-Cursor', 100)
render json: [find_table]
if params[:show_columns]
render json: [find_table('42')]
else
render json: [find_table]
end
end

def show
Expand Down Expand Up @@ -53,6 +57,24 @@ def find_table(id = nil)
created_at: time.iso8601,
updated_at: time.iso8601,
}
when '42'
{
id: 42,
name: 'access',
description: 'logs',
database: {
id: 4242,
name: 'production'
},
columns: [
{ name: 'id', column_type: 'integer' },
{ name: 'description', column_type: 'varchar' }
],
null_sample: nil,
storage_size: 12.3,
created_at: time.iso8601,
updated_at: time.iso8601,
}
else
raise NotFoundError
end
Expand Down
25 changes: 23 additions & 2 deletions spec/rails/doc/smart/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ paths:
schema:
type: integer
example: 10
- name: show_columns
in: query
schema:
type: string
example: "true"
responses:
'200':
description: with flat query parameters
Expand All @@ -46,16 +51,21 @@ paths:
items:
"$ref": "#/components/schemas/Table"
example:
- id: 1
- id: 42
name: access
description: logs
database:
id: 2
id: 4242
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'
columns:
- name: id
column_type: integer
- name: description
column_type: varchar
headers:
X-Cursor:
schema:
Expand Down Expand Up @@ -137,6 +147,10 @@ components:
type: string
database:
"$ref": "#/components/schemas/Database"
columns:
type: array
items:
"$ref": "#/components/schemas/Column"
null_sample:
nullable: true
storage_size:
Expand All @@ -155,3 +169,10 @@ components:
description: 'this should be preserved'
name:
type: string
Column:
type: object
properties:
name:
type: string
column_type:
type: string
4 changes: 4 additions & 0 deletions spec/rails/doc/smart/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ components:
type: string
database:
"$ref": "#/components/schemas/Database"
columns:
type: array
items:
"$ref": "#/components/schemas/Column"
null_sample:
nullable: true
storage_size:
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/rails_smart_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
context it 'returns a list of tables' do
it 'with flat query parameters' do
# These new params replace them in old spec
get '/tables', params: { page: '42', per: '10' },
get '/tables', params: { page: '42', per: '10', show_columns: true },
headers: { authorization: 'k0kubun', "X-Authorization-Token": 'token' }
response.set_header('X-Cursor', 100)
expect(response.status).to eq(200)
Expand Down