Skip to content

Commit

Permalink
Merge pull request #685 from bitgopatmcl/fix-ref-titles
Browse files Browse the repository at this point in the history
Use `allOf` for schemas that are refs
  • Loading branch information
andrew-scott-fischer authored Feb 23, 2024
2 parents 9aaaad1 + 0321fa9 commit 0b5bf99
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ export function convertRoutesToOpenAPI(
const openapiSchema = schemaToOpenAPI(schema);
if (openapiSchema === undefined) {
return acc;
} else if ('$ref' in openapiSchema) {
return {
...acc,
[name]: {
allOf: [{ title: name }, openapiSchema],
},
};
} else {
return {
...acc,
Expand Down
149 changes: 147 additions & 2 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
parseRoute,
Project,
type Route,
type Schema,
} from '../src';

async function testCase(
Expand All @@ -23,6 +24,7 @@ async function testCase(

const project = new Project();
const routes: Route[] = [];
const schemas: Record<string, Schema> = {};
const errors: string[] = [];
for (const symbol of sourceFile.symbols.declarations) {
if (symbol.init !== undefined) {
Expand All @@ -36,7 +38,7 @@ async function testCase(
}
const result = parseRoute(project, routeSchemaE.right);
if (E.isLeft(result)) {
errors.push(result.left);
schemas[symbol.name] = routeSchemaE.right;
} else {
routes.push(result.right);
}
Expand All @@ -46,7 +48,7 @@ async function testCase(
const actual = convertRoutesToOpenAPI(
{ title: 'Test', version: '1.0.0' },
routes,
{},
schemas,
);

assert.deepStrictEqual(errors, expectedErrors);
Expand Down Expand Up @@ -538,3 +540,146 @@ testCase('object with no required properties', EMPTY_REQUIRED, {
schemas: {},
},
});

const SCHEMA_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: Foo,
}),
response: {
/** foo response */
200: t.string
},
});
const Foo = t.type({ foo: t.string });
`;

testCase('request body ref', SCHEMA_REF, {
openapi: '3.0.0',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
get: {
parameters: [],
requestBody: {
content: {
'application/json': {
schema: {
$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 SCHEMA_DOUBLE_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: Bar,
}),
response: {
/** foo response */
200: t.string
},
});
const Foo = t.type({ foo: t.string });
const Bar = Foo;
`;

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

0 comments on commit 0b5bf99

Please sign in to comment.