Skip to content

Commit

Permalink
fix(schema): fix issue when oneOf & discriminator have no children class
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jan 11, 2023
1 parent 0a82fad commit 2488f60
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/specs/schema/src/decorators/common/oneOf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ describe("@OneOf", () => {
type: "object"
});
});
it("should declare return schema for only one model", () => {
// WHEN
class Nested {
@Property()
id: string;
}
class Model {
@OneOf(Nested)
num: string;
}

// THEN
const schema = getJsonSchema(Model);

expect(schema).toEqual({
definitions: {
Nested: {
properties: {
id: {
type: "string"
}
},
type: "object"
}
},
properties: {
num: {
$ref: "#/definitions/Nested"
}
},
type: "object"
});
});
it("should declare two models", () => {
class One1 {
@Property()
Expand Down
4 changes: 4 additions & 0 deletions packages/specs/schema/src/domain/JsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ export class JsonSchema extends Map<string, any> implements NestedGenerics {

const children = resolvedOneOf[0].discriminator().children();

if (!children.length) {
return this.type(oneOf[0]);
}

resolvedOneOf = children.map(mapToJsonSchema);
}

Expand Down
72 changes: 72 additions & 0 deletions packages/specs/schema/test/discriminator.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Required,
Returns
} from "@tsed/schema";
import Base = Mocha.reporters.Base;

class Event {
@DiscriminatorKey() // declare this property a discriminator key
Expand Down Expand Up @@ -782,6 +783,77 @@ describe("Discriminator", () => {
]
});
});
it("should generate the spec (return no item)", () => {
class Base {
@DiscriminatorKey() // declare this property a discriminator key
type: string;

@Property()
value: string;
}

@Controller("/")
class MyTest {
@Get("/:id")
@Returns(200).OneOf(Base)
get(@PathParams(":id") id: string) {
return [];
}
}

expect(getSpec(MyTest)).toEqual({
components: {
schemas: {
Base: {
properties: {
type: {
type: "string"
},
value: {
type: "string"
}
},
type: "object"
}
}
},
paths: {
"/{id}": {
get: {
operationId: "myTestGet",
parameters: [
{
in: "path",
name: "id",
required: true,
schema: {
type: "string"
}
}
],
responses: {
"200": {
content: {
"*/*": {
schema: {
$ref: "#/components/schemas/Base"
}
}
},
description: "Success"
}
},
tags: ["MyTest"]
}
}
},
tags: [
{
name: "MyTest"
}
]
});
});
});
describe("isDiscriminatorChild", () => {
it("should return true when it's a child discriminator", () => {
Expand Down

0 comments on commit 2488f60

Please sign in to comment.