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

[Example] make sure all versions use the same shared components$ref #198

Closed
wants to merge 1 commit into from

Conversation

acunniffe
Copy link
Contributor

@acunniffe acunniffe commented Mar 8, 2022

[Example Only] -- do not merge

You all have shared that you plan to put shared schema on GitHub and let users consume them using $ref

This has some challenges I think using JSON Schema Refs differently could solve.

  • Long URLs are hard to read
  • If people are copy / pasting, they might end up with inconsistent versions ie one URL uses tag 1.4 and another URL uses tag 1.5
  • It's difficult to update every $ref at once

This is a really simple example where we import your shared schemas to an extension ie x-v3-schemas and then, everywhere else, resolve those via #/x-v3-schemas/Target

Right now your shared schemas are still spread across multiple files. To make this work best, I'd suggest having one "entry point" that follows the standard components section OpenAPI conventions:

  • parameters
  • schemas
  • responses

https://swagger.io/docs/specification/components/

This probably just means adding a registry-like schema that $ref out to all the subcomponents you want in the shared schemas users import.

@acunniffe acunniffe requested a review from a team as a code owner March 8, 2022 14:59
@acunniffe acunniffe marked this pull request as draft March 8, 2022 14:59
@acunniffe acunniffe changed the title Example of a way to make sure all versions use the same shared components$ref [Example] make sure all versions use the same shared components$ref Mar 8, 2022
@@ -9,6 +9,9 @@ servers:
tags:
- name: Thing
description: Short description of what Thing represents
x-v3-schemas:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one time import of your shared schemas

@@ -9,6 +9,9 @@ servers:
tags:
- name: Thing
description: Short description of what Thing represents
x-v3-schemas:
$ref: https://raw.githubusercontent.com/snyk/sweater-comb/v1.5.0/components/parameters/version.yaml
# ^ tag
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify the tag in one place, for every subsequent $ref

@@ -18,7 +21,7 @@ paths:
tags:
- Thing
parameters:
- { $ref: "../../../../../components/parameters/version.yaml#/Version" }
- { $ref: '#/x-v3-schemas/Version' }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usage is a lot cleaner

cmars added a commit to cmars/sweater-comb that referenced this pull request Mar 8, 2022
This reorganizes common components into a single components file so that
it may be referenced once in an OpenAPI spec using JSON References.

End-to-end tests are updated to use the updated references.

The component name rule is adjusted to allow this change. It might be
tightened up in a followup to check the names of component leaf-nodes.

Credit for this awesome idea to @acunniffe, originally proposed in snyk#198
as a proof-of-concept.
cmars added a commit to cmars/sweater-comb that referenced this pull request Mar 8, 2022
This reorganizes common components into a single components file so that
it may be referenced once in an OpenAPI spec using JSON References.

End-to-end tests are updated to use the updated references.

The component name rule is adjusted to allow this change. It might be
tightened up in a followup to check the names of component leaf-nodes.

Credit for this awesome idea to @acunniffe, originally proposed in snyk#198
as a proof-of-concept.
cmars added a commit to cmars/sweater-comb that referenced this pull request Mar 8, 2022
This reorganizes common components into a single components file so that
it may be referenced once in an OpenAPI spec using JSON References.

End-to-end tests are updated to use the updated references.

The component name rule is adjusted to allow this change. It might be
tightened up in a followup to check the names of component leaf-nodes.

Credit for this awesome idea to @acunniffe, originally proposed in snyk#198
as a proof-of-concept.
@cmars
Copy link
Contributor

cmars commented Mar 15, 2022

Closing as this is superseded by #199. Thanks again for the example!

@cmars cmars closed this Mar 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants