Skip to content

Commit

Permalink
Merge pull request #73 from lifeomic/PML-222
Browse files Browse the repository at this point in the history
fix(PML-222): unexpected type in generated openapi spec
  • Loading branch information
Shawn Zhu authored Jul 12, 2023
2 parents 96f59c9 + 3ca9baf commit 8bae395
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ const TEST_SPEC: OneSchemaDefinition = withAssumptions({
type: 'string',
optional: true,
},
limit: {
description: 'page size',
type: 'integer',
optional: true,
},
updatedAt: {
description: 'epoch time',
type: 'number',
optional: true,
},
},
},
Response: {
Expand Down Expand Up @@ -132,6 +142,20 @@ describe('toOpenAPISpec', () => {
required: false,
schema: { type: 'string' },
},
{
in: 'query',
description: 'page size',
name: 'limit',
required: false,
schema: { type: 'integer' },
},
{
in: 'query',
description: 'epoch time',
name: 'updatedAt',
required: false,
schema: { type: 'number' },
},
],
responses: {
'200': {
Expand Down
21 changes: 20 additions & 1 deletion src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { OpenAPIV3 } from 'openapi-types';
import type { OneSchemaDefinition } from './types';
import { deepCopy } from './generate-endpoints';
import { validateSchema } from './meta-schema';
import { JSONSchema4 } from 'json-schema';

/**
* Converts e.g. `/users/:id/profile` to `/users/{id}/profile`.
Expand All @@ -18,6 +19,24 @@ const getPathParameters = (koaPath: string) =>
.filter((part) => part.startsWith(':'))
.map((part) => part.slice(1));

/**
* @param schema json schema object
* @return supported openapi schema object type
*/
const getSchemaObjectType = (
schema: JSONSchema4,
): OpenAPIV3.NonArraySchemaObjectType => {
// TODO supports more types from JSON schema
switch (schema.type) {
case 'integer':
return 'integer';
case 'number':
return 'number';
default:
return 'string';
}
};

export const toOpenAPISpec = (
schema: OneSchemaDefinition,
config: {
Expand Down Expand Up @@ -87,7 +106,7 @@ export const toOpenAPISpec = (
in: 'query',
name,
description: schema.description,
schema: { type: 'string' },
schema: { type: getSchemaObjectType(schema) },
required:
Array.isArray(Request.required) &&
Request.required.includes(name),
Expand Down

0 comments on commit 8bae395

Please sign in to comment.