Skip to content

Commit

Permalink
#/components/schemas/ can be omitted to ease usage
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Aug 24, 2022
1 parent a0280c9 commit 07ca178
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ Yes, rspec-openapi v0.7.0+ supports [`$ref` mechanism](https://swagger.io/docs/s
schemas under `#/components/schemas` with some manual steps.

1. First, generate plain OpenAPI file.
2. Then, manually replace the duplications with `$ref` and define the placeholder of schema under
`#/components/schemas/` section. Example:
2. Then, manually replace the duplications with `$ref`.

```yaml
paths:
Expand All @@ -190,12 +189,10 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/User"
components:
schemas:
# No need to define User manually
# Note) #/components/schamas is not needed to be defined.
```

3. Then, re-run rspec-openapi. It tries to find the the referenced schema (`User` for example) and update `properties`.
3. Then, re-run rspec-openapi. It will generate `#/components/schemas` with the referenced schema (`User` for example) newly-generated or updated.

```yaml
paths:
Expand Down
5 changes: 2 additions & 3 deletions lib/rspec/openapi/components_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ class << RSpec::OpenAPI::ComponentsUpdater = Object.new
# @param [Hash] base
# @param [Hash] fresh
def update!(base, fresh)
return if (base_schemas = base.dig('components', 'schemas')).nil?

# Top-level schema: Used as the body of request or response
top_level_refs = paths_to_top_level_refs(base)
return if top_level_refs.empty?
fresh_schemas = build_fresh_schemas(top_level_refs, base, fresh)

# Nested schema: References in Top-level schemas. May contain some top-level schema.
Expand All @@ -26,7 +25,7 @@ def update!(base, fresh)
RSpec::OpenAPI::SchemaMerger.merge!(fresh_schemas[schema_name], nested_schema)
end

RSpec::OpenAPI::SchemaMerger.merge!(base_schemas, fresh_schemas)
RSpec::OpenAPI::SchemaMerger.merge!(base, { 'components' => { 'schemas' => fresh_schemas }})
RSpec::OpenAPI::SchemaCleaner.cleanup_components_schemas!(base, { 'components' => { 'schemas' => fresh_schemas } })
end

Expand Down

0 comments on commit 07ca178

Please sign in to comment.