- Cleanup of generation without tags

This commit is contained in:
Ferdi Koomen 2022-01-24 18:43:14 +01:00
parent 5a8457fee8
commit 40ef68fec4
6 changed files with 55 additions and 12 deletions

View File

@ -16,7 +16,7 @@ export interface OpenApiOperation {
produces?: string[];
parameters?: OpenApiParameter[];
responses: OpenApiResponses;
schemes: ('http' | 'https' | 'ws' | 'wss')[];
schemes?: ('http' | 'https' | 'ws' | 'wss')[];
deprecated?: boolean;
security?: OpenApiSecurityRequirement[];
}

View File

@ -0,0 +1,31 @@
import { getServices } from './getServices';
describe('getServices', () => {
it('should create a unnamed service if tags are empty', () => {
const services = getServices({
swagger: '2.0',
info: {
title: 'x',
version: '1',
},
paths: {
'/api/trips': {
get: {
tags: [],
responses: {
200: {
description: 'x',
},
default: {
description: 'default',
},
},
},
},
},
});
expect(services).toHaveLength(1);
expect(services[0].name).toEqual('');
});
});

View File

@ -28,7 +28,7 @@ export function getServices(openApi: OpenApi): Service[] {
case 'patch':
// Each method contains an OpenAPI operation, we parse the operation
const op = path[method]!;
const tags = op.tags?.filter(unique) || ['Service'];
const tags = op.tags?.length ? op.tags.filter(unique) : [''];
tags.forEach(tag => {
const operation = getOperation(openApi, url, method, tag, op, pathParams);

View File

@ -3,16 +3,29 @@ import { getServices } from './getServices';
describe('getServices', () => {
it('should create a unnamed service if tags are empty', () => {
const services = getServices({
openapi: '3',
info: { title: 'x', version: '1' },
openapi: '3.0.0',
info: {
title: 'x',
version: '1',
},
paths: {
'/api/trips': {
get: { tags: [], responses: { 200: { description: 'X' }, default: { description: 'default' } } },
get: {
tags: [],
responses: {
200: {
description: 'x',
},
default: {
description: 'default',
},
},
},
},
},
});
expect(services).toHaveLength(1);
expect(services[0].name).toEqual('Unnamed');
expect(services[0].name).toEqual('');
});
});

View File

@ -28,8 +28,7 @@ export function getServices(openApi: OpenApi): Service[] {
case 'patch':
// Each method contains an OpenAPI operation, we parse the operation
const op = path[method]!;
const uniqueTags = op.tags?.filter(unique);
const tags = uniqueTags?.length ? uniqueTags : ['Unnamed'];
const tags = op.tags?.length ? op.tags.filter(unique) : [''];
tags.forEach(tag => {
const operation = getOperation(openApi, url, method, tag, op, pathParams);

View File

@ -272,7 +272,7 @@ function base64(str: string): string {
function getQueryString(params: Record<string, any>): string {
const searchParams = new URLSearchParams();
const process = (key:string, value: any) => {
const process = (key: string, value: any) => {
if (isDefined(value)) {
if (Array.isArray(value)) {
value.forEach(v => {
@ -326,7 +326,7 @@ function getFormData(options: ApiRequestOptions): FormData | undefined {
.filter(([_, value]) => isDefined(value))
.forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach(v => append(key, v));
value.forEach(v => process(key, v));
} else {
process(key, value);
}
@ -3121,7 +3121,7 @@ function base64(str: string): string {
function getQueryString(params: Record<string, any>): string {
const searchParams = new URLSearchParams();
const process = (key:string, value: any) => {
const process = (key: string, value: any) => {
if (isDefined(value)) {
if (Array.isArray(value)) {
value.forEach(v => {
@ -3175,7 +3175,7 @@ function getFormData(options: ApiRequestOptions): FormData | undefined {
.filter(([_, value]) => isDefined(value))
.forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach(v => append(key, v));
value.forEach(v => process(key, v));
} else {
process(key, value);
}