Skip to content

feat: better support for polymorphism#116

Merged
erunion merged 11 commits intomasterfrom
feat/better-polymorphism-support
Mar 6, 2020
Merged

feat: better support for polymorphism#116
erunion merged 11 commits intomasterfrom
feat/better-polymorphism-support

Conversation

@erunion
Copy link
Member

@erunion erunion commented Feb 27, 2020

find-schema-definition

  • Slightly rewriting the tool so it can handle $ref pointers that are escaped with ~1 by just using the great jsonpointer library.
    • This is mostly the same code that RJSF uses, just without the recursion to call find-schema-definition if we find a nested $ref. We handle that case in the code that handles find-schema-definition in the Explorer, and though they're probably ripe for a rewrite it's fine for now.

flatten-schema

  • Added support for flattening a schema that contains oneOf, allOf, and anyOf polymorphism cases.
    • For oneOf and anyOf cases, since we can't really flatten down every option defined in those arrays into a single schema that would validate against the document, I'm choosing to just select the first defined object in the set and flatten that out.
  • Making a minor adjustment to only attempt to flatten an object if it's actually an object. Previously, we'd throw up a fatal exception on a schema that looks like this:
"application/xml": {
  "schema": "~paths~/pet~post~requestBody~content~application/json~schema"
}

parameters-to-json-schema

  • Made updates to stop storing our component schemas inside of a definitions object as a recent push to RJSF made this no longer a requirement.

@erunion erunion added the enhancement New feature or request label Feb 27, 2020
@erunion erunion marked this pull request as ready for review March 2, 2020 23:40
@erunion erunion requested a review from domharrington March 5, 2020 00:42
@erunion erunion added the bug Something isn't working label Mar 5, 2020
Copy link
Member

@domharrington domharrington left a comment

Choose a reason for hiding this comment

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

👐 few questions in-line, but it looks great on the whole.

@@ -0,0 +1,3 @@
{
"extends": ["@commitlint/config-conventional"]
Copy link
Member

Choose a reason for hiding this comment

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

👀 what does this do?

Copy link
Member Author

@erunion erunion Mar 6, 2020

Choose a reason for hiding this comment

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

Helps conform commits to a common standard https://commitlint.js.org/. I've been manually adopting this in our OSS projects because it helps to keep changelogs and release notes consistent, this just formalizes that here.


test('should return a definition for a given ref that is escaped', () => {
expect(
findSchemaDefinition('#/components/schemas/Pet~1Error', {
Copy link
Member

Choose a reason for hiding this comment

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

Is this pattern of escaping actually what happens in openapi? Is this supported? Pet~1Error

Copy link
Member Author

@erunion erunion Mar 6, 2020

Choose a reason for hiding this comment

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

Yeah, to have a a schema, Pet/Error, with a slash in it you need to escape the / as ~1, otherwise trying to access #/components/schemas/Pet/Error would try to load:

components: { schemas: { Pet: { Error: { ... } } } }

jsonpointer handles this all for us. 🚀

https://swagger.io/docs/specification/using-ref/#escape

});

describe('polymorphism cases', () => {
describe('allOf', () => {
Copy link
Member

Choose a reason for hiding this comment

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

So flattenSchema is for use in the response sidebar? 👌

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that's the only place it's used.

});

describe('anyOf', () => {
it('should flatten only the first schema listed', () => {
Copy link
Member

Choose a reason for hiding this comment

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

This is good for now! We can work on this in future to allow selection in the UI.

label: types[type],
schema: oas.components
? { definitions: { components: cleanupSchemaDefaults(oas.components) }, ...cleanupSchemaDefaults(schema.schema) }
? { components: cleanupSchemaDefaults(oas.components), ...cleanupSchemaDefaults(schema.schema) }
Copy link
Member

Choose a reason for hiding this comment

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

Is this how you're supposed to do it in OAS? Did I misread the spec?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope! This change was needed because the version of your #/definitions handling in our fork got merged last week. rjsf-team/react-jsonschema-form#1506

RJSF can now properly access #/components/schemas/... without trying to think that they should be located under #/definitions/components/schemas/....

@erunion erunion merged commit 1975251 into master Mar 6, 2020
@erunion erunion deleted the feat/better-polymorphism-support branch March 6, 2020 17:13
erunion pushed a commit that referenced this pull request Feb 7, 2025
Bumps [@commitlint/config-conventional](https://github.com/conventional-changelog/commitlint/tree/HEAD/@commitlint/config-conventional) from 17.0.2 to 17.0.3.
- [Release notes](https://github.com/conventional-changelog/commitlint/releases)
- [Changelog](https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/config-conventional/CHANGELOG.md)
- [Commits](https://github.com/conventional-changelog/commitlint/commits/v17.0.3/@commitlint/config-conventional)

---
updated-dependencies:
- dependency-name: "@commitlint/config-conventional"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants