Check for polymorphic child schemas in search for request schemas

This commit is contained in:
Mike Kistler 2023-02-09 13:23:39 -08:00
Родитель 253e5bdedd
Коммит a6c446c3d4
2 изменённых файлов: 55 добавлений и 1 удалений

Просмотреть файл

@ -1,11 +1,13 @@
// Flag any properties that are readonly in the response schema.
// Scan an OpenAPI document to determine if a schema is a response-only schema,
// which means it is not references by any request schemas.
// which means it is not referenced by any request schemas.
// Any schema that is referenced by a request is considered a request schema.
// Any schema referenced by a request schema is also considered a request schema.
// Any schema that "allOf"'s a request schema with a discriminator is also a request schema.
// Any schema that is not a request schema is considered a response-only schema.
// requestSchemas is a set of schema names that we have determined are request schemas
let requestSchemas;
function getRequestSchemas(oasDoc) {
@ -64,6 +66,17 @@ function getRequestSchemas(oasDoc) {
}
}
}
if (schema.discriminator) {
// Check all the schemas in the document and add any that "allOf" this schema
// into schemasToProcess
const schemaRef = `#/definitions/${schemaName}`;
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of Object.entries(oasDoc.definitions)) {
if (value.allOf?.some((elem) => elem.$ref === schemaRef)) {
schemasToProcess.push(key);
}
}
}
}
}
}

Просмотреть файл

@ -93,6 +93,24 @@ test('az-readonly-in-response-schema should not find errors', () => {
},
},
},
'/test2': {
post: {
parameters: [
{
in: 'body',
name: 'body',
schema: {
$ref: '#/definitions/Pet',
},
},
],
responses: {
200: {
description: 'Success',
},
},
},
},
},
definitions: {
Model1: {
@ -155,6 +173,29 @@ test('az-readonly-in-response-schema should not find errors', () => {
},
},
},
Pet: {
discriminator: 'petType',
},
Dog: {
type: 'object',
allOf: [{ $ref: '#/definitions/Pet' }],
properties: {
cute: {
type: 'number',
readOnly: true,
},
},
},
Cat: {
type: 'object',
allOf: [{ $ref: '#/definitions/Pet' }],
properties: {
attitude: {
type: 'string',
readOnly: true,
},
},
},
},
};
return linter.run(oasDoc).then((results) => {