Skip to content

Commit

Permalink
Merge pull request #759 from BitGo/DX-376-wrap-ref-in-allOf
Browse files Browse the repository at this point in the history
fix(openapi-generator): wrap nullable $ref parameters in an 'allOf' array
  • Loading branch information
bitgopatmcl authored May 7, 2024
2 parents ce392a6 + 56b0b09 commit b4d0fec
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ function schemaToOpenAPI(
if (oneOf.length === 0) {
return undefined;
} else if (oneOf.length === 1) {
return { ...(nullable ? { nullable } : {}), ...oneOf[0] };
if (
Object.keys(
oneOf[0] as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
)[0] === '$ref'
)
// OpenAPI spec doesn't allow $ref properties to have siblings, so they're wrapped in an 'allOf' array
return { ...(nullable ? { nullable } : {}), allOf: oneOf };
else return { ...(nullable ? { nullable } : {}), ...oneOf[0] };
} else {
return { ...(nullable ? { nullable } : {}), oneOf };
}
Expand Down
74 changes: 74 additions & 0 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,80 @@ testCase('request body double ref', SCHEMA_DOUBLE_REF, {
},
});

const SCHEMA_NULLABLE_REF = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
export const route = h.httpRoute({
path: '/foo',
method: 'GET',
request: t.type({
body: t.union([Foo, t.null]),
}),
response: {
/** foo response */
200: t.string
},
});
const Foo = t.type({ foo: t.string });
`;

testCase('request body nullable ref', SCHEMA_NULLABLE_REF, {
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
get: {
parameters: [],
requestBody: {
content: {
'application/json': {
schema: {
nullable: true,
allOf: [
{
$ref: '#/components/schemas/Foo',
},
],
},
},
},
},
responses: {
200: {
description: 'foo response',
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
},
},
components: {
schemas: {
Foo: {
title: 'Foo',
type: 'object',
properties: {
foo: {
type: 'string',
},
},
required: ['foo'],
},
},
},
});

const TITLE_TAG = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
Expand Down

0 comments on commit b4d0fec

Please sign in to comment.