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: exclude readonly params from deprecatedProps #561

Merged
merged 2 commits into from
Dec 14, 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
8 changes: 8 additions & 0 deletions __tests__/__datasets__/schema-deprecated.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@
"$ref": "#/components/schemas/Tag"
}
},
"idReadOnly": {
"deprecated": true,
"readOnly": true,
"type": "integer",
"format": "int64",
"default": 40,
"example": 25
},
"tags_alt": {
"description": "Unlike the `tags` parameter, this is **not** deprecated, but the contents within itself are.",
"type": "array",
Expand Down
10 changes: 10 additions & 0 deletions __tests__/operation/get-parameters-as-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,16 @@ describe('deprecated', () => {
});
expect(Object.keys(deprecatedSchema.properties)).toHaveLength(4);
});

it('should not put readOnly deprecated parameters in deprecatedProps', async () => {
const oas = new Oas(deprecated);
await oas.dereference();
const operation = oas.operation('/anything', 'post');
const deprecatedSchema = operation.getParametersAsJsonSchema()[1].deprecatedProps.schema;

expect(Object.keys(deprecatedSchema.properties)).toHaveLength(4);
expect('idReadOnly' in Object.keys(deprecatedSchema.properties)).toBe(false);
});
});

describe('request bodies', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/operation/get-parameters-as-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ export default (path: string, operation: OperationObject, api: OASDocument, glob
// Booleans are not valid for required in draft 4, 7 or 2020. Not sure why the typing thinks they are.
const requiredParams = (schema.required || []) as Array<string>;

// Find all top-level deprecated properties from the schema - required params are excluded
// Find all top-level deprecated properties from the schema - required and readOnly params are excluded
const allDeprecatedProps: { [key: string]: SchemaObject } = {};

Object.keys(deprecatedBody.properties).forEach(key => {
const deprecatedProp = deprecatedBody.properties[key] as SchemaObject;
if (deprecatedProp.deprecated && !requiredParams.includes(key)) {
if (deprecatedProp.deprecated && !requiredParams.includes(key) && !deprecatedProp.readOnly) {
allDeprecatedProps[key] = deprecatedProp;
}
});
Expand Down