Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/sour-baboons-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-ts-json-schema": patch
---

Fix JSON Schema conversions on objects with `type` prop
39 changes: 16 additions & 23 deletions src/utils/convertOpenApiToJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,23 @@ function convertToJsonSchema<Value extends unknown>(
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);
Expand Down
24 changes: 24 additions & 0 deletions test/unit/convertOpenApiToJsonSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});