Skip to content

Commit

Permalink
fix: fix fastify swagger error when properties attribute is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnig committed Mar 29, 2022
1 parent 803ac3f commit 3052609
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 1,080 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/schema/generateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import $RefParser from '@apidevtools/json-schema-ref-parser';
import * as ts from 'ts-json-schema-generator';
import { Config } from 'ts-json-schema-generator';
import { log } from '../log.js';
import { addProperties } from './helper/add-properties.js';
import { convertEmptyObject } from './helper/convertEmptyObject.js';
import { convertNullToNullable } from './helper/convertNullToNullable.js';

Expand Down Expand Up @@ -30,10 +31,10 @@ export class GenerateGlobalSchema {
let schema = ts.createGenerator(config).createSchema() as any;

schema = addId(schema.definitions);

schema = convertNullToNullable(schema);

schema = convertEmptyObject(schema);
schema = addProperties(schema);

return JSON.stringify(schema, null, 2);
} catch (e: any) {
if (e.message.includes('NoRootNamesError')) {
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/schema/helper/add-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// schemas without properties cause fastify-swagger to fail
export function addProperties(definitions: Record<string, any>) {
Object.keys(definitions).forEach((key: string) => {
const definition = definitions[key];
if (definition.type === 'object' && !definition.properties) {
definition.properties = {};
}
});

return definitions;
}
3 changes: 2 additions & 1 deletion packages/integration-tests/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { flux } from '@fluxapi/common';
import { openapi } from '@fluxapi/plugins';
import { CrudController } from './crud/crud.controller';
import { InputController } from './input/input.controller';
import { ResponseController } from './response/response.controller';

const fastify = flux({
plugins: [openapi()],
controllers: [ResponseController, CrudController],
controllers: [ResponseController, CrudController, InputController],
});

fastify.listen(8080, '127.0.0.1', (err, address) => {
Expand Down
10 changes: 10 additions & 0 deletions packages/integration-tests/src/input/input.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Controller, Get } from '@fluxapi/common';
import { InputEmptyQuery } from './input.schema';

@Controller('/inputs', { tags: ['responses'] })
export class InputController {
@Get('/empty-query')
async emptyQuery(query: InputEmptyQuery): Promise<void> {
return;
}
}
1 change: 1 addition & 0 deletions packages/integration-tests/src/input/input.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface InputEmptyQuery {}
14 changes: 13 additions & 1 deletion packages/integration-tests/src/response/response.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Controller, Delete, Get, Status } from '@fluxapi/common';
import { AnyResponse, NullResponse, ObjectResponse, AddionalResponse, UndefinedResponse } from './response.schema';
import {
AnyResponse,
NullResponse,
ObjectResponse,
AddionalResponse,
UndefinedResponse,
Foobar,
} from './response.schema';

@Controller('/responses', { tags: ['responses'] })
export class ResponseController {
Expand Down Expand Up @@ -65,4 +72,9 @@ export class ResponseController {
objectNullUndefined: null,
};
}

@Get('/empty-interface-response')
async emptyInterfaceResponse(): Promise<Foobar> {
return { foo: 'bar' };
}
}
2 changes: 2 additions & 0 deletions packages/integration-tests/src/response/response.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export interface UndefinedResponse {
objectNullUndefined?: { id: number; name: string } | null;
objectNullUndefined2?: { id: number; name: string } | null;
}

export interface Foobar {}
Loading

0 comments on commit 3052609

Please sign in to comment.