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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions __tests__/tooling/operation/get-parameters-as-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,56 @@ describe('request bodies', () => {
});

describe('polymorphism / inheritance', () => {
describe('adding missing `type` properties', () => {
it("should not add a `type` to a shapeless-description that's part of an `allOf`", () => {
const oas = createOas({
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
petIds: {
allOf: [{ type: 'array', items: { type: 'string' } }, { description: 'Parameter description' }],
},
},
},
},
},
},
});

const schema = oas.operation('/', 'get').getParametersAsJsonSchema();

expect(schema[0].schema.properties.petIds.allOf[1].type).toBeUndefined();
});

it.each([['anyOf'], ['oneOf']])("should add a `type` to a shapeless-description that's part of an `%s`", prop => {
const oas = createOas({
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
petIds: {
[prop]: [{ type: 'array', items: { type: 'string' } }, { description: 'Parameter description' }],
},
},
},
},
},
},
});

const schema = oas.operation('/', 'get').getParametersAsJsonSchema();
expect(schema[0].schema.properties.petIds[prop][1]).toStrictEqual({
description: 'Parameter description',
type: 'string',
});
});
});

it.each([['allOf'], ['anyOf'], ['oneOf']])('should support nested %s', prop => {
const oas = createOas({
requestBody: {
Expand Down
28 changes: 25 additions & 3 deletions tooling/operation/get-parameters-as-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,17 @@ function searchForExampleByPointer(pointer, examples = []) {
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md
* @param {Object} data
* @param {Object[]} prevSchemas
* @param {string} currentLocation
* @param {String} currentLocation
* @param {Object} globalDefaults
* @param {Boolean} isPolymorphicAllOfChild
*/
function constructSchema(data, prevSchemas = [], currentLocation = '', globalDefaults) {
function constructSchema(
data,
prevSchemas = [],
currentLocation = '',
globalDefaults,
isPolymorphicAllOfChild = false
) {
const schema = { ...data };

// If this schema contains a `$ref`, it's circular and we shouldn't try to resolve it. Just return and move along.
Expand All @@ -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.

// If this schema is immediate child of a polymorphic schema and is neither an array or an object, we should
// leave it alone. Cases like this are common where somebody might use `allOf` in order to dynamically add a
// `description` onto another schema, like such:
//
// allOf: [
// { type: 'array', items: { type: 'string' },
// { description: 'This is the description for the `array`.' }
// ]
} else {
// If we're processing a schema that has no types, no refs, and is just a lone schema, we should treat it at the
// bare minimum as a simple string so we make an attempt to generate valid JSON Schema.
Expand Down Expand Up @@ -330,7 +346,13 @@ function constructSchema(data, prevSchemas = [], currentLocation = '', globalDef
['allOf', 'anyOf', 'oneOf'].forEach(polyType => {
if (polyType in schema && Array.isArray(schema[polyType])) {
schema[polyType].forEach((item, idx) => {
schema[polyType][idx] = constructSchema(item, prevSchemas, `${currentLocation}/${idx}`, globalDefaults);
schema[polyType][idx] = constructSchema(
item,
prevSchemas,
`${currentLocation}/${idx}`,
globalDefaults,
polyType === 'allOf'
);
});
}
});
Expand Down