Skip to content

Commit 30a6a4c

Browse files
author
Ansh Chaturvedi
committed
feat: support @minValue and @maxValue
Ticket: DX-447
1 parent 7b853d9 commit 30a6a4c

File tree

2 files changed

+129
-9
lines changed

2 files changed

+129
-9
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@ function schemaToOpenAPI(
1414
const createOpenAPIObject = (
1515
schema: Schema,
1616
): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined => {
17-
const description = schema.comment?.description;
18-
const example = getTagName(schema, 'example');
19-
const pattern = getTagName(schema, 'pattern');
20-
21-
const defaultOpenAPIObject = {
22-
...(description ? { description } : {}),
23-
...(example ? { example } : {}),
24-
...(pattern ? { pattern } : {}),
25-
};
17+
const defaultOpenAPIObject = buildDefaultOpenAPIObject(schema);
2618

2719
switch (schema.type) {
2820
case 'boolean':
@@ -141,6 +133,23 @@ function schemaToOpenAPI(
141133
}
142134
};
143135

136+
function buildDefaultOpenAPIObject(schema: Schema): OpenAPIV3.SchemaObject {
137+
const description = schema.comment?.description;
138+
const example = getTagName(schema, 'example');
139+
const maxLength = getTagName(schema, 'maxLength');
140+
const minLength = getTagName(schema, 'minLength');
141+
const pattern = getTagName(schema, 'pattern');
142+
143+
const defaultOpenAPIObject = {
144+
...(description ? { description } : {}),
145+
...(example ? { example } : {}),
146+
...(maxLength ? { maxLength: Number(maxLength) } : {}),
147+
...(minLength ? { minLength: Number(minLength) } : {}),
148+
...(pattern ? { pattern } : {}),
149+
};
150+
return defaultOpenAPIObject;
151+
}
152+
144153
const titleObject = schema.comment?.tags.find((t) => t.tag === 'title');
145154

146155
let openAPIObject = createOpenAPIObject(schema);

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,3 +2326,114 @@ testCase('route with descriptions for references', ROUTE_WITH_DESCRIPTIONS_FOR_R
23262326
}
23272327
}
23282328
});
2329+
2330+
const ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS = `
2331+
import * as t from 'io-ts';
2332+
import * as h from '@api-ts/io-ts-http';
2333+
2334+
/**
2335+
* A simple route with type descriptions for references
2336+
*
2337+
* @operationId api.v1.test
2338+
* @tag Test Routes
2339+
*/
2340+
export const route = h.httpRoute({
2341+
path: '/foo',
2342+
method: 'GET',
2343+
request: h.httpRequest({
2344+
query: {
2345+
bar: t.array(t.string),
2346+
},
2347+
body: {
2348+
/**
2349+
* This is a foo description.
2350+
* @minLength 5
2351+
* @maxLength 10
2352+
* @example "BitgoInc"
2353+
*/
2354+
foo: t.string()
2355+
},
2356+
}),
2357+
response: {
2358+
200: {
2359+
test: t.string
2360+
}
2361+
},
2362+
});
2363+
`;
2364+
2365+
testCase('route with min and max values for strings', ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS, {
2366+
openapi: '3.0.3',
2367+
info: {
2368+
title: 'Test',
2369+
version: '1.0.0'
2370+
},
2371+
paths: {
2372+
'/foo': {
2373+
get: {
2374+
summary: 'A simple route with type descriptions for references',
2375+
operationId: 'api.v1.test',
2376+
tags: [
2377+
'Test Routes'
2378+
],
2379+
parameters: [
2380+
{
2381+
name: 'bar',
2382+
in: 'query',
2383+
required: true,
2384+
schema: {
2385+
type: 'array',
2386+
items: {
2387+
type: 'string'
2388+
}
2389+
}
2390+
}
2391+
],
2392+
requestBody: {
2393+
content: {
2394+
'application/json': {
2395+
schema: {
2396+
type: 'object',
2397+
properties: {
2398+
foo: {
2399+
type: 'string',
2400+
description: 'This is a foo description.',
2401+
example: 'BitgoInc',
2402+
minLength: 5,
2403+
maxLength: 10
2404+
}
2405+
},
2406+
required: [
2407+
'foo'
2408+
]
2409+
}
2410+
}
2411+
}
2412+
},
2413+
responses: {
2414+
'200': {
2415+
description: 'OK',
2416+
content: {
2417+
'application/json': {
2418+
schema: {
2419+
type: 'object',
2420+
properties: {
2421+
test: {
2422+
type: 'string'
2423+
}
2424+
},
2425+
required: [
2426+
'test'
2427+
]
2428+
}
2429+
}
2430+
}
2431+
}
2432+
}
2433+
}
2434+
}
2435+
},
2436+
components: {
2437+
schemas: {}
2438+
}
2439+
});

0 commit comments

Comments
 (0)