Skip to content

Commit

Permalink
Merge pull request #785 from BitGo/DX-448-add-@default-support
Browse files Browse the repository at this point in the history
feat: add `@default` support
  • Loading branch information
bitgopatmcl authored Jun 6, 2024
2 parents 026ebd8 + 25eeb9d commit 5ed5860
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ function schemaToOpenAPI(
};

function buildDefaultOpenAPIObject(schema: Schema): OpenAPIV3.SchemaObject {
const defaultValue = getTagName(schema, 'default');
const description = schema.comment?.description;
const example = getTagName(schema, 'example');
const maxLength = getTagName(schema, 'maxLength');
const minLength = getTagName(schema, 'minLength');
const pattern = getTagName(schema, 'pattern');

const defaultOpenAPIObject = {
...(defaultValue ? { default: defaultValue } : {}),
...(description ? { description } : {}),
...(example ? { example } : {}),
...(maxLength ? { maxLength: Number(maxLength) } : {}),
Expand Down
10 changes: 6 additions & 4 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2327,7 +2327,7 @@ testCase('route with descriptions for references', ROUTE_WITH_DESCRIPTIONS_FOR_R
}
});

const ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS = `
const ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS_AND_DEFAULT = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
Expand All @@ -2349,7 +2349,8 @@ export const route = h.httpRoute({
* This is a foo description.
* @minLength 5
* @maxLength 10
* @example "BitgoInc"
* @example "SomeInc"
* @default "BitgoInc"
*/
foo: t.string()
},
Expand All @@ -2362,7 +2363,7 @@ export const route = h.httpRoute({
});
`;

testCase('route with min and max values for strings', ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS, {
testCase('route with min and max values for strings and default value', ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS_AND_DEFAULT, {
openapi: '3.0.3',
info: {
title: 'Test',
Expand Down Expand Up @@ -2398,7 +2399,8 @@ testCase('route with min and max values for strings', ROUTE_WITH_MIN_AND_MAX_VAL
foo: {
type: 'string',
description: 'This is a foo description.',
example: 'BitgoInc',
example: 'SomeInc',
default: 'BitgoInc',
minLength: 5,
maxLength: 10
}
Expand Down

0 comments on commit 5ed5860

Please sign in to comment.