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
2 changes: 1 addition & 1 deletion packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function schemaToOpenAPI(
},
{} as Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>,
),
required: schema.required,
...(schema.required.length > 0 ? { required: schema.required } : {}),
};
case 'intersection':
return {
Expand Down
70 changes: 70 additions & 0 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,73 @@ testCase('source file with a header comment', HEADER_COMMENT, {
schemas: {},
},
});

const EMPTY_REQUIRED = `
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: h.httpRequest({
body: {
foo: t.string,
},
}),
response: {
/** foo response */
200: t.partial({ foo: t.string })
},
});
`;

// Test that `required` is not emitted as an empty array
testCase('object with no required properties', EMPTY_REQUIRED, {
openapi: '3.0.0',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
get: {
parameters: [],
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
required: ['foo'],
},
},
},
},
responses: {
200: {
description: 'foo response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
foo: {
type: 'string',
},
},
},
},
},
},
},
},
},
},
components: {
schemas: {},
},
});