Skip to content

Commit

Permalink
Merge pull request #788 from BitGo/DX-456-add-more-tags-in-openapi-ge…
Browse files Browse the repository at this point in the history
…nerator

feat: add support for more tags in `openapi-generator`
  • Loading branch information
bitgopatmcl authored Jun 6, 2024
2 parents 27b380a + ebf6e8a commit 8eb75bf
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ function schemaToOpenAPI(
const maxLength = getTagName(schema, 'maxLength');
const minLength = getTagName(schema, 'minLength');
const pattern = getTagName(schema, 'pattern');
const minimum = getTagName(schema, 'minimum');
const maximum = getTagName(schema, 'maximum');
const minItems = getTagName(schema, 'minItems');
const maxItems = getTagName(schema, 'maxItems');
const minProperties = getTagName(schema, 'minProperties');
const maxProperties = getTagName(schema, 'maxProperties');
const exclusiveMinimum = getTagName(schema, 'exclusiveMinimum');
const exclusiveMaximum = getTagName(schema, 'exclusiveMaximum');
const multipleOf = getTagName(schema, 'multipleOf');
const uniqueItems = getTagName(schema, 'uniqueItems');
const readOnly = getTagName(schema, 'readOnly');
const writeOnly = getTagName(schema, 'writeOnly');
const format = getTagName(schema, 'format');

const deprecated = schema.comment?.tags.find((t) => t.tag === 'deprecated');
Expand All @@ -152,6 +164,18 @@ function schemaToOpenAPI(
...(maxLength ? { maxLength: Number(maxLength) } : {}),
...(minLength ? { minLength: Number(minLength) } : {}),
...(pattern ? { pattern } : {}),
...(minimum ? { minimum: Number(minimum) } : {}),
...(maximum ? { maximum: Number(maximum) } : {}),
...(minItems ? { minItems: Number(minItems) } : {}),
...(maxItems ? { maxItems: Number(maxItems) } : {}),
...(minProperties ? { minProperties: Number(minProperties) } : {}),
...(maxProperties ? { maxProperties: Number(maxProperties) } : {}),
...(exclusiveMinimum ? { exclusiveMinimum: true } : {}),
...(exclusiveMaximum ? { exclusiveMaximum: true } : {}),
...(multipleOf ? { multipleOf: Number(multipleOf) } : {}),
...(uniqueItems ? { uniqueItems: true } : {}),
...(readOnly ? { readOnly: true } : {}),
...(writeOnly ? { writeOnly: true } : {}),
...(format ? { format } : {}),
};
return defaultOpenAPIObject;
Expand Down
114 changes: 114 additions & 0 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2531,3 +2531,117 @@ testCase('route with deprecated tag', ROUTE_WITH_DEPRECATED_TAG, {
schemas: {}
}
});

const ROUTE_WITH_MIN_MAX_AND_OTHER_TAGS = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
/**
* A simple route with type descriptions for references
*
* @operationId api.v1.test
* @tag Test Routes
*/
export const route = h.httpRoute({
path: '/foo',
method: 'GET',
request: h.httpRequest({
body: {
/**
* This is a foo description.
* @minimum 5
* @maximum 10
* @minItems 1
* @maxItems 5
* @minProperties 1
* @maxProperties 500
* @exclusiveMinimum true
* @exclusiveMaximum true
* @multipleOf 7
* @uniqueItems true
* @readOnly true
* @writeOnly true
*/
foo: t.number()
},
}),
response: {
200: {
test: t.string
}
},
});
`;

testCase('route with min and max tags', ROUTE_WITH_MIN_MAX_AND_OTHER_TAGS, {
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0'
},
paths: {
'/foo': {
get: {
summary: 'A simple route with type descriptions for references',
operationId: 'api.v1.test',
parameters: [],
tags: [
'Test Routes'
],
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
foo: {
type: 'number',
description: 'This is a foo description.',
minimum: 5,
maximum: 10,
minItems: 1,
maxItems: 5,
minProperties: 1,
multipleOf: 7,
maxProperties: 500,
exclusiveMinimum: true,
exclusiveMaximum: true,
uniqueItems: true,
readOnly: true,
writeOnly: true
}
},
required: [
'foo'
]
}
}
}
},
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
test: {
type: 'string'
}
},
required: [
'test'
]
}
}
}
}
}
}
}
},
components: {
schemas: {}
}
});

0 comments on commit 8eb75bf

Please sign in to comment.