Untitled

 avatar
unknown
typescript
9 months ago
3.9 kB
11
Indexable
    it('should to generate a basic OpenAPI document with get endpoint with defaults', () => {

        class Product {
            @IsString()
            @IsUUID()
            declare uuid: string

            @IsString()
            declare name: string;

            @IsOptional()
            @IsCurrency()
            declare price: string;
        }

        @Collection(Product)
        class Products extends Array<Product> {}

        @ApiContract({
            title: 'Products api',
            version: '2.0.0',
        })
        class MyApiClass {

            @Get()
            getProducts(): Products {
                return null as unknown as Products;
            };

            @Post()
            createProduct(): Product {
                return null as unknown as Product;
            }

            @Post('/clearcache')
            clearCache() {};
        }

        const openAPIDoc = generateOpenAPI(MyApiClass, {
            defaultResponseType: 'application/json',
        });

        expect(openAPIDoc).toEqual({
            openapi: '3.0.3',
            info: {
                title: 'Products api',
                version: '2.0.0',
            },
            paths: {
                '/': {
                    get: {
                        operationId: 'getProducts',
                        responses: {
                            '200': {
                                description: 'OK',
                                content: {
                                    'application/json': {
                                        schema: {
                                            $ref: '#/components/schemas/Products',
                                        },
                                    },
                                },
                            },
                        },
                    },
                    post: {
                        operationId: 'createProduct',
                        responses: {
                            '200': {
                                description: 'OK',
                                content: {
                                    'application/json': {
                                        schema: {
                                            $ref: '#/components/schemas/Product',
                                        },
                                    },
                                },
                            },
                        },
                    },
                },
                '/clearcache': {
                    post: {
                        operationId: 'clearCache',
                        responses: {
                            '204': {
                                description: 'No Content',
                            },
                        },
                    }
                },
            },
            components: {
                schemas: {
                    Product: {
                        type: 'object',
                        properties: {
                            uuid: {
                                type: 'string',
                                format: 'uuid',
                            },
                            name: {
                                type: 'string',
                            },
                            price: {
                                type: 'string',
                                format: 'currency', // No currency format in OpenAPI
                            },
                        },
                        required: ['uuid', 'name'],
                    },
                    Products: {
                        type: 'array',
                        items: {
                            $ref: '#/components/schemas/Product',
                        },
                    },
                },
                securitySchemes: {},
            },
        });
    });
Editor is loading...
Leave a Comment