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

fix: edge case where allOfs would receive a type when one wasn't present #374

Merged
merged 1 commit into from
Feb 18, 2021

Conversation

erunion
Copy link
Member

@erunion erunion commented Feb 18, 2021

🧰 What's being changed?

@rafegoldberg stumbled on this in the AWS Workspaces definition while testing out readmeio/api-explorer#1179, but we have an edge case right now where if you're using allOf to dynamically add a description, and only that, to another schema, we'll add type: string to the description object.

For example:

allOf: [
  { 
    type: 'array', 
    items: { 
      type: 'string' 
    }
  }, 
  { description: 'Parameter description' } // This ends up receiving `type: string`.
],

Reason why this ends up failing in the API Explorer is because when @readme/oas-form attempts to merge the schemas in the allOf, array and string collide and json-schema-merge-allof throws an error and instead of merging the schemas together @readme/oas-form instead returns the original schemas sans the allOf -- resulting in a type-less schema.

I continue to be eternally grateful that I took the time to rewrite getParametersAsJsonSchema in December because fixing this kind of thing in the old code would have been a Hot Mess.

🧪 Testing

See attached unit tests, but here's the before and after of this fix in the API Explorer:

Before

Screen Shot 2021-02-17 at 6 40 43 PM

After

Screen Shot 2021-02-17 at 6 41 31 PM

@erunion erunion added the bug Something isn't working label Feb 18, 2021
@@ -197,6 +204,15 @@ function constructSchema(data, prevSchemas = [], currentLocation = '', globalDef
schema.type = 'object';
} else if ('items' in schema) {
schema.type = 'array';
} else if (isPolymorphicAllOfChild) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Since this shapeless case only happens with an allOf where each schema within that block is merged together, we shouldn't apply this logic to oneOf and anyOf polymorphic schemas.

Copy link
Contributor

Choose a reason for hiding this comment

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

am I reading this right that we're just leaving this with an empty if block?

Is this preferred over switching the final else to else if not polymporphic? I'm not sure I have an opinion just yet.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this is just a no-op. Figured it was cleaner and easier to read this way.

Copy link

@rafegoldberg rafegoldberg left a comment

Choose a reason for hiding this comment

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

Damn, the turnaround between my comment and this PR was quick af. I haven't run this code, but it code reads well and test cases check out per your screenshots/description of the issue!

@erunion erunion merged commit 07cd8ef into master Feb 18, 2021
@erunion erunion deleted the fix/allof-shapeless-descriptions branch February 18, 2021 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants