Skip to content

Commit

Permalink
refactor: don't pull required deprecated params out to deprecatedProps (
Browse files Browse the repository at this point in the history
#531)

* refactor: dont pull required deprecated params

* chore: remove sneaky little space >:'(

* chore: cleanup
  • Loading branch information
julshotal authored Oct 29, 2021
1 parent c40e0eb commit f29856e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,6 @@ https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#paramet
],
"type": "string",
},
"status": Object {
"deprecated": true,
"items": Object {
"default": "available",
"deprecated": true,
"enum": Array [
"available",
"pending",
"sold",
],
"type": "string",
},
"type": "array",
},
},
"required": Array [
"status",
Expand All @@ -141,6 +127,20 @@ https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#paramet
],
"type": "string",
},
"status": Object {
"deprecated": true,
"items": Object {
"default": "available",
"deprecated": true,
"enum": Array [
"available",
"pending",
"sold",
],
"type": "string",
},
"type": "array",
},
},
"required": Array [
"status",
Expand Down Expand Up @@ -186,24 +186,6 @@ https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-
"minimum": -9223372036854776000,
"type": "integer",
},
"name": Object {
"deprecated": true,
"examples": Array [
"doggie",
],
"type": "string",
},
"photoUrls": Object {
"deprecated": true,
"items": Object {
"deprecated": true,
"examples": Array [
"https://example.com/photo.png",
],
"type": "string",
},
"type": "array",
},
"status": Object {
"deprecated": true,
"description": "pet status in the store",
Expand Down Expand Up @@ -264,6 +246,24 @@ https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-
"type": "object",
"x-readme-ref-name": "Category",
},
"name": Object {
"deprecated": true,
"examples": Array [
"doggie",
],
"type": "string",
},
"photoUrls": Object {
"deprecated": true,
"items": Object {
"deprecated": true,
"examples": Array [
"https://example.com/photo.png",
],
"type": "string",
},
"type": "array",
},
"polymorphism": Object {
"properties": Object {
"allOf": Object {
Expand Down
16 changes: 13 additions & 3 deletions __tests__/operation/get-parameters-as-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ describe('deprecated', () => {
pathId: {
name: 'pathId',
in: 'path',
required: true,
deprecated: true,
schema: {
type: 'integer',
Expand All @@ -648,7 +647,6 @@ describe('deprecated', () => {
);

await oas.dereference();

expect(oas.operation('/', 'get').getParametersAsJsonSchema()[0].deprecatedProps.schema).toStrictEqual({
type: 'object',
properties: {
Expand All @@ -660,7 +658,7 @@ describe('deprecated', () => {
deprecated: true,
},
},
required: ['pathId'],
required: [],
});
});

Expand All @@ -671,6 +669,18 @@ describe('deprecated', () => {

expect(operation.getParametersAsJsonSchema()).toMatchSnapshot();
});

it('should not put required 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;

deprecatedSchema.required.forEach(requiredParam => {
expect(requiredParam in deprecatedSchema.properties).toBe(false);
});
expect(Object.keys(deprecatedSchema.properties)).toHaveLength(4);
});
});

describe('request bodies', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/operation/get-parameters-as-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ module.exports = (path, operation, oas, globalDefaults = {}) => {
if (!schema || !schema.properties) return null;
// Clone the original schema so this doesn't interfere with it
const deprecatedBody = cloneObject(schema);
const requiredParams = schema.required || [];

// Find all top-level deprecated properties from the schema
// Find all top-level deprecated properties from the schema - required params are excluded
const allDeprecatedProps = {};
Object.keys(deprecatedBody.properties).forEach(key => {
if (deprecatedBody.properties[key].deprecated) {
if (deprecatedBody.properties[key].deprecated && !requiredParams.includes(key)) {
allDeprecatedProps[key] = deprecatedBody.properties[key];
}
});
Expand All @@ -57,7 +58,7 @@ module.exports = (path, operation, oas, globalDefaults = {}) => {
// Remove deprecated properties from the original schema
// Not using the clone here becuase we WANT this to affect the original
Object.keys(schema.properties).forEach(key => {
if (schema.properties[key].deprecated) delete schema.properties[key];
if (schema.properties[key].deprecated && !requiredParams.includes(key)) delete schema.properties[key];
});

return {
Expand Down

0 comments on commit f29856e

Please sign in to comment.