Skip to content

Commit

Permalink
Merge pull request #826 from BitGo/DX-531
Browse files Browse the repository at this point in the history
feat: add support for array examples with the @arrayExample tag
  • Loading branch information
anshchaturvedi authored Jul 17, 2024
2 parents 6515011 + 374a19f commit 5e35eb3
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import type { Route } from './route';
import type { Schema } from './ir';
import { Block } from 'comment-parser';

type ExtendedOpenApiSchema = OpenAPIV3.SchemaObject & {
arrayExample?: string;
};

function schemaToOpenAPI(
schema: Schema,
): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined {
Expand Down Expand Up @@ -50,7 +54,14 @@ function schemaToOpenAPI(
if (innerSchema === undefined) {
return undefined;
}
return { type: 'array', items: { ...innerSchema, ...defaultOpenAPIObject } };

const { arrayExample, ...rest } = defaultOpenAPIObject;

return {
type: 'array',
...(arrayExample ? { example: JSON.parse(arrayExample) } : {}), // Add example to array if it exists
items: { ...innerSchema, ...rest },
};
case 'object':
return {
type: 'object',
Expand Down Expand Up @@ -173,7 +184,7 @@ function schemaToOpenAPI(
}
};

function buildDefaultOpenAPIObject(schema: Schema): OpenAPIV3.SchemaObject {
function buildDefaultOpenAPIObject(schema: Schema): ExtendedOpenApiSchema {
const emptyBlock: Block = { description: '', tags: [], source: [], problems: [] };
const jsdoc = parseCommentBlock(schema.comment ?? emptyBlock);

Expand All @@ -196,6 +207,7 @@ function schemaToOpenAPI(
const writeOnly = jsdoc?.tags?.writeOnly ?? schema.writeOnly;
const format = jsdoc?.tags?.format ?? schema.format ?? schema.format;
const title = jsdoc?.tags?.title ?? schema.title;
const arrayExample = jsdoc?.tags?.arrayExample ?? '';

const deprecated =
Object.keys(jsdoc?.tags || {}).includes('deprecated') || !!schema.deprecated;
Expand Down Expand Up @@ -223,7 +235,9 @@ function schemaToOpenAPI(
...(writeOnly ? { writeOnly: true } : {}),
...(format ? { format } : {}),
...(title ? { title } : {}),
...(arrayExample ? { arrayExample } : {}),
};

return defaultOpenAPIObject;
}

Expand Down
109 changes: 109 additions & 0 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3567,3 +3567,112 @@ testCase("route with titles in request bodies", SCHEMA_WITH_TITLES_IN_REQUEST_BO
}
}
});


const ROUTE_WITH_ARRAY_EXAMPLE = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
export const route = h.httpRoute({
path: '/foo',
method: 'POST',
request: h.httpRequest({
params: {},
body: t.type({
/**
* @example "btc"
*/
array1: t.array(t.string),
/**
* @example "btc"
* @arrayExample ["btc", "eth"]
*/
array2: t.array(t.string),
objectWithArray: t.type({
/**
* @arrayExample ["btc", "eth"]
*/
nestedArray: t.array(t.string)
})
})
}),
response: {
200: t.literal('OK'),
},
});`

testCase("route with array examples", ROUTE_WITH_ARRAY_EXAMPLE, {
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0'
},
paths: {
'/foo': {
post: {
parameters: [],
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
array1: {
type: 'array',
items: {
type: 'string',
example: '"btc"'
},
},
array2: {
type: 'array',
example: ['btc', 'eth'],
items: {
type: 'string',
example: '"btc"'
},
},
objectWithArray: {
properties: {
nestedArray: {
example: [
'btc',
'eth'
],
items: {
type: 'string'
},
type: 'array'
}
},
required: [
'nestedArray'
],
type: 'object'
},
},
required: [ 'array1', 'array2', 'objectWithArray' ],
},
}
}
},
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string',
enum: [ 'OK' ]
}
}
}
}
}
}
}
},
components: {
schemas: {}
}
});

0 comments on commit 5e35eb3

Please sign in to comment.