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

Allow oneOf to be parsed. RE: #5 #81

Merged
merged 6 commits into from
Jun 6, 2017

Conversation

brendo
Copy link
Contributor

@brendo brendo commented Jun 2, 2017

This MR updates schemaParser to be able to consume oneOf definitions (using a helper, resolveOneOf).

It works by creating full permutations of the object by applying each of the possible oneOf states.

I expect performance will degrade on large schemas, but given we only run the schema parsing at a property level, I think this should be sufficient for the short term.

I have added three test cases:

  • A "required" oneOf, which is identical to our scenario for authentication with the Temando Platform API
  • Multiple type scenario, also taken from the Temando Platform API. Note that this is a little convoluted because it could otherwise be defined using type: [ array, of, types]
  • A combination of both of the above in the same schema, to test that we can recursively apply the permutations

Other:

  • Added a test helper, getTestsFromFixtures that helps load a set of inputs/outputs from a directory
  • Minor optimisations in schemaParser. Most objects in this logic are created as the result of a JSON.parse, so the checks to hasOwnProperty are redundant

Note this MR does introduce visualising of the oneOf schema, that'll be handled in #32.

@brendo brendo changed the title WIP: Allow oneOf to be parsed Allow oneOf to be parsed. RE: #5 Jun 5, 2017
* @param {object} obj
* @return {array}
*/
function getOneOfPaths (obj) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!
If you want, you could rename k to key. It would be more descriptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

const resolvedJsonSchema = resolveAllOf(jsonSchema)
const outputSchema = getPropertiesNode(resolvedJsonSchema.properties, resolvedJsonSchema.required)
let resolved = resolveAllOf(jsonSchema)
resolved = resolveOneOf(jsonSchema)
Copy link
Contributor

@quangkhoa quangkhoa Jun 6, 2017

Choose a reason for hiding this comment

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

It looks like the result of resolveAllOf is discarded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice find!

"required": ["foo"]
}
]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would reorder the elements in this file for better readability. Currently the order is as follows:

  • string - bar
  • integer - foo
  • integer - bar
  • string - foo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

return obj
}

const states = getStates(paths, obj)
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about permutations, as opposed to states?

* If no `oneOf` keys were found, an empty array is returned.
*
* @param {object} obj
* @return {array}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be {string[]} paths to be consistent with the docs on getStates function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@quangkhoa quangkhoa merged commit 32bbdc3 into temando:master Jun 6, 2017
@brendo brendo deleted the issue-5-one-of-parser branch June 13, 2017 00:18
brendo pushed a commit to brendo/open-api-renderer that referenced this pull request Jun 20, 2017
brendo pushed a commit to brendo/open-api-renderer that referenced this pull request Jun 20, 2017
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.

None yet

2 participants