Skip to content

Commit

Permalink
Merge pull request #805 from BitGo/DX-463-schema-titles
Browse files Browse the repository at this point in the history
test: add tests to check for schema tags
  • Loading branch information
bitgopatmcl authored Jun 17, 2024
2 parents 07f8ebe + b3be7f9 commit dd18f46
Showing 1 changed file with 226 additions and 118 deletions.
344 changes: 226 additions & 118 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ async function testCase(
throw new Error('Failed to parse source file');
}


const project = new Project();
const routes: Route[] = [];
const schemas: Record<string, Schema> = {};
Expand Down Expand Up @@ -1619,143 +1618,143 @@ export const route = h.httpRoute({
});
`;

testCase('route with mixed types and descriptions', ROUTE_WITH_MIXED_TYPES_AND_DESCRIPTIONS,
{
openapi: "3.0.3",
info: {
title: "Test",
version: "1.0.0"
},
paths: {
'/foo': {
get: {
summary: "A simple route with type descriptions",
operationId: "api.v1.test",
tags: [
"Test Routes"
],
parameters: [
{
name: "bar",
description: "bar param",
in: "query",
required: true,
schema: {
type: "string"
}
}
],
requestBody: {
content: {
'application/json': {
testCase('route with mixed types and descriptions', ROUTE_WITH_MIXED_TYPES_AND_DESCRIPTIONS,
{
openapi: "3.0.3",
info: {
title: "Test",
version: "1.0.0"
},
paths: {
'/foo': {
get: {
summary: "A simple route with type descriptions",
operationId: "api.v1.test",
tags: [
"Test Routes"
],
parameters: [
{
name: "bar",
description: "bar param",
in: "query",
required: true,
schema: {
type: "object",
properties: {
foo: {
type: "string",
description: "description to describe an optional string"
},
bar: {
oneOf: [
{
type: "number"
},
{
type: "string"
}
],
description: "description to describe an optional union of number and string"
},
child: {
type: "object",
description: "description to describe an object",
properties: {
child: {
type: "object",
description: "dsecription to describe an intersection of a type and a partial",
properties: {
foo: {
type: "string"
},
bar: {
type: "number"
}
type: "string"
}
}
],
requestBody: {
content: {
'application/json': {
schema: {
type: "object",
properties: {
foo: {
type: "string",
description: "description to describe an optional string"
},
bar: {
oneOf: [
{
type: "number"
},
required: [
"foo"
]
}
{
type: "string"
}
],
description: "description to describe an optional union of number and string"
},
required: [
"child"
]
},
error: {
type: "object",
description: "description to describe a t.type",
properties: {
error: {
type: "string"
}
child: {
type: "object",
description: "description to describe an object",
properties: {
child: {
type: "object",
description: "dsecription to describe an intersection of a type and a partial",
properties: {
foo: {
type: "string"
},
bar: {
type: "number"
}
},
required: [
"foo"
]
}
},
required: [
"child"
]
},
required: [
"error"
]
},
obj: {
type: "object",
description: "description to describe an optional t.object",
properties: {}
error: {
type: "object",
description: "description to describe a t.type",
properties: {
error: {
type: "string"
}
},
required: [
"error"
]
},
obj: {
type: "object",
description: "description to describe an optional t.object",
properties: {}
},
exact: {
type: "object",
description: "description to describe a t.exact",
properties: {
foo: {
type: "string"
}
},
required: [
"foo"
]
}
},
exact: {
required: [
"child",
"error",
"exact"
]
}
}
}
},
responses: {
200: {
description: "OK",
content: {
'application/json': {
schema: {
type: "object",
description: "description to describe a t.exact",
properties: {
foo: {
test: {
type: "string"
}
},
required: [
"foo"
"test"
]
}
},
required: [
"child",
"error",
"exact"
]
}
}
}
},
responses: {
200: {
description: "OK",
content: {
'application/json': {
schema: {
type: "object",
properties: {
test: {
type: "string"
}
},
required: [
"test"
]
}
}
}
}
}
}
},
components: {
schemas: {}
}
},
components: {
schemas: {}
}
});
});

const ROUTE_WITH_ARRAY_TYPES_AND_DESCRIPTIONS = `
import * as t from 'io-ts';
Expand Down Expand Up @@ -2826,3 +2825,112 @@ testCase('route with array union of null and undefined', ROUTE_WITH_ARRAY_UNION_
schemas: {}
}
});

const ROUTE_WITH_SCHEMA_WITH_COMMENT = `
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({}),
response: {
200: SimpleRouteResponse,
400: ApiError
},
});
/**
* Human readable description of the Simple Route Response
* @title Human Readable Simple Route Response
*/
const SimpleRouteResponse = t.type({
test: t.string,
});
/**
* Human readable description of the ApiError schema
* @title Human Readable Api Error Schema
*/
const ApiError = t.type({
error: t.string,
});
`

testCase('route with api error schema', ROUTE_WITH_SCHEMA_WITH_COMMENT, {
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',
tags: [
'Test Routes'
],
parameters: [],
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: {
'$ref': '#/components/schemas/SimpleRouteResponse'
}
}
}
},
'400': {
content: {
'application/json': {
schema: {
'$ref': '#/components/schemas/ApiError'
}
}
},
description: 'Bad Request'
}
}
}
},
},
components: {
schemas: {
ApiError: {
properties: {
error: {
type: 'string'
}
},
required: [
'error'
],
title: 'Human Readable Api Error Schema',
description: 'Human readable description of the ApiError schema',
type: 'object'
},
SimpleRouteResponse: {
description: 'Human readable description of the Simple Route Response',
properties: {
test: {
type: 'string'
}
},
required: [
'test'
],
title: 'Human Readable Simple Route Response',
type: 'object'
}
},
},
});

0 comments on commit dd18f46

Please sign in to comment.