Skip to content

Commit be80f8b

Browse files
Merge pull request #672 from bitgopatmcl/no-empty-required
Don't emit empty `required` property arrays
2 parents dc4be7e + c8e82a5 commit be80f8b

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function schemaToOpenAPI(
4444
},
4545
{} as Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>,
4646
),
47-
required: schema.required,
47+
...(schema.required.length > 0 ? { required: schema.required } : {}),
4848
};
4949
case 'intersection':
5050
return {

packages/openapi-generator/test/openapi.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,73 @@ testCase('source file with a header comment', HEADER_COMMENT, {
468468
schemas: {},
469469
},
470470
});
471+
472+
const EMPTY_REQUIRED = `
473+
import * as t from 'io-ts';
474+
import * as h from '@api-ts/io-ts-http';
475+
476+
export const route = h.httpRoute({
477+
path: '/foo',
478+
method: 'GET',
479+
request: h.httpRequest({
480+
body: {
481+
foo: t.string,
482+
},
483+
}),
484+
response: {
485+
/** foo response */
486+
200: t.partial({ foo: t.string })
487+
},
488+
});
489+
`;
490+
491+
// Test that `required` is not emitted as an empty array
492+
testCase('object with no required properties', EMPTY_REQUIRED, {
493+
openapi: '3.0.0',
494+
info: {
495+
title: 'Test',
496+
version: '1.0.0',
497+
},
498+
paths: {
499+
'/foo': {
500+
get: {
501+
parameters: [],
502+
requestBody: {
503+
content: {
504+
'application/json': {
505+
schema: {
506+
type: 'object',
507+
properties: {
508+
foo: {
509+
type: 'string',
510+
},
511+
},
512+
required: ['foo'],
513+
},
514+
},
515+
},
516+
},
517+
responses: {
518+
200: {
519+
description: 'foo response',
520+
content: {
521+
'application/json': {
522+
schema: {
523+
type: 'object',
524+
properties: {
525+
foo: {
526+
type: 'string',
527+
},
528+
},
529+
},
530+
},
531+
},
532+
},
533+
},
534+
},
535+
},
536+
},
537+
components: {
538+
schemas: {},
539+
},
540+
});

0 commit comments

Comments
 (0)