diff --git a/.changeset/sour-baboons-check.md b/.changeset/sour-baboons-check.md new file mode 100644 index 0000000..3c738f3 --- /dev/null +++ b/.changeset/sour-baboons-check.md @@ -0,0 +1,5 @@ +--- +"openapi-ts-json-schema": patch +--- + +Fix JSON Schema conversions on objects with `type` prop diff --git a/src/utils/convertOpenApiToJsonSchema.ts b/src/utils/convertOpenApiToJsonSchema.ts index eb2ec9d..04c5aa1 100644 --- a/src/utils/convertOpenApiToJsonSchema.ts +++ b/src/utils/convertOpenApiToJsonSchema.ts @@ -18,30 +18,23 @@ function convertToJsonSchema( return value; } - /** - * type as array is not a valid OpenAPI value - * https://swagger.io/docs/specification/data-models/data-types#mixed-types - */ - if (Array.isArray(value.type)) { - return value; - } - - /** - * Skip parameter objects - */ - if ('in' in value) { - return value; - } + if ('type' in value) { + /** + * Skip entities with "type" props defined and not a string + * (They should have already been converted, anyway) + * https://github.com/toomuchdesign/openapi-ts-json-schema/issues/211 + */ + if (typeof value.type !== 'string') { + return value; + } - /** - * Skip security scheme object definitions - * https://swagger.io/specification/#security-scheme-object - */ - if ( - typeof value.type === 'string' && - SECURITY_SCHEME_OBJECT_TYPES.includes(value.type) - ) { - return value; + /** + * Skip security scheme object definitions + * https://swagger.io/specification/#security-scheme-object + */ + if (SECURITY_SCHEME_OBJECT_TYPES.includes(value.type)) { + return value; + } } const schema = fromSchema(value); diff --git a/test/unit/convertOpenApiToJsonSchema.test.ts b/test/unit/convertOpenApiToJsonSchema.test.ts index 7bddedf..d09bacd 100644 --- a/test/unit/convertOpenApiToJsonSchema.test.ts +++ b/test/unit/convertOpenApiToJsonSchema.test.ts @@ -94,5 +94,29 @@ describe('convertOpenApiToJsonSchema', () => { expect(actual).toEqual(definition); }); }); + + describe('Object with "type" prop (#211)', () => { + it('convert object definitions', () => { + const actual = convertOpenApiToJsonSchema({ + type: 'object', + properties: { + type: { type: 'string', nullable: true }, + bar: { type: 'string' }, + }, + required: ['type', 'bar'], + }); + + const expected = { + type: 'object', + properties: { + type: { type: ['string', 'null'] }, + bar: { type: 'string' }, + }, + required: ['type', 'bar'], + }; + + expect(actual).toEqual(expected); + }); + }); }); });