Skip to content

Commit

Permalink
fix(swagger): remove the generation of useless builtin types (#963)
Browse files Browse the repository at this point in the history
fix(swagger): Remove the generation of useless builtin types

- Add tests to ensure this mistakes won't happen

Closes: #921
  • Loading branch information
rluvaton committed Oct 7, 2020
1 parent 4b01850 commit 4e224e1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/swagger/src/class/OpenApiModelSchemaBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {JsonSchema, JsonSchemesRegistry, PropertyMetadata} from "@tsed/common";
import {isArrayOrArrayClass, isClass, isCollection, isObject, nameOf, Storable, Store, Type} from "@tsed/core";
import {isArrayOrArrayClass, isClass, isCollection, isObject, isPrimitiveOrPrimitiveClass, nameOf, Storable, Store, Type} from "@tsed/core";
import {Schema} from "swagger-schema-official";
import {OpenApiDefinitions} from "../interfaces/OpenApiDefinitions";
import {OpenApiResponses} from "../interfaces/OpenApiResponses";
Expand Down Expand Up @@ -32,6 +32,11 @@ export class OpenApiModelSchemaBuilder {
* @returns {OpenApiModelSchemaBuilder}
*/
build(): this {
// Stop from creating primitive schemas (#921)
if (isPrimitiveOrPrimitiveClass(this.target)) {
return this;
}

const properties = PropertyMetadata.getProperties(this.target);
const store = Store.from(this.target);
const schema: Schema = this.getClassSchema();
Expand Down
24 changes: 24 additions & 0 deletions packages/swagger/src/class/OpenApiParamsBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,30 @@ describe("OpenApiParamsBuilder", () => {
}
});
});

it("should create a swagger schema from BodyParams with String type", () => {
// GIVEN
class MyCtrl {
test(@BodyParams() test: string) {}
}

// WHEN
const builder = new OpenApiParamsBuilder(MyCtrl, "test").build();

// THEN
expect(builder.parameters).to.deep.eq([
{
description: "",
in: "body",
required: false,
name: "body",
schema: {
type: "string"
}
}
]);
});

it("should create query params", () => {
class ParameterModel {
@Property()
Expand Down
32 changes: 31 additions & 1 deletion packages/swagger/test/swagger.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Controller, Description, Get, MergeParams, PathParams, PlatformTest} from "@tsed/common";
import {BodyParams, ContentType, Controller, Description, Get, Post, MergeParams, PathParams, PlatformTest} from "@tsed/common";
import {expect} from "chai";
import * as SuperTest from "supertest";
import {Docs, Hidden, Returns, ReturnsArray} from "../src";
Expand Down Expand Up @@ -44,6 +44,12 @@ class CalendarsController {
async getAll(): Promise<Calendar[]> {
return [new Calendar({id: 1, name: "name"}), new Calendar({id: 2, name: "name"})];
}

@ContentType("text/plain")
@Post("/csv")
async csv(@BodyParams() csvLines: string): Promise<string> {
return "";
}
}

describe("Swagger integration", () => {
Expand Down Expand Up @@ -156,6 +162,30 @@ describe("Swagger integration", () => {
},
tags: ["CalendarsController"]
}
},
"/rest/calendars/csv": {
post: {
operationId: "CalendarsController.csv",
parameters: [
{
in: "body",
name: "body",
required: false,
schema: {
type: "string"
}
}
],
produces: [
"text/plain"
],
responses: {
"200": {
description: "Success"
}
},
tags: ["CalendarsController"]
}
}
},
produces: ["application/json"],
Expand Down

0 comments on commit 4e224e1

Please sign in to comment.