Skip to content

Commit

Permalink
fix(schema): set the correct content type when OneOf/AnyOf/AllOf is u…
Browse files Browse the repository at this point in the history
…sed on Returns decorator
  • Loading branch information
Romakita committed Jun 21, 2024
1 parent 3a1d7d8 commit 0aabf62
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Object {
"responses": Object {
"200": Object {
"content": Object {
"*/*": Object {
"application/json": Object {
"schema": Object {
"discriminator": Object {
"propertyName": "type",
Expand Down Expand Up @@ -294,7 +294,7 @@ Object {
"responses": Object {
"200": Object {
"content": Object {
"*/*": Object {
"application/json": Object {
"schema": Object {
"discriminator": Object {
"propertyName": "type",
Expand Down
77 changes: 77 additions & 0 deletions packages/specs/schema/src/decorators/operations/returns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,83 @@ describe("@Returns", () => {
]
});
});
it("should apply application/json content type if OneOf/AnyOf/AllOf is used without base model", () => {
class Model {
@Property()
id: string;
}

class Model2 {
@Property()
id: string;
}

class Controller {
@OperationPath("POST", "/")
@Returns(200).OneOf(Model, Model2).Description("description")
method() {
return {};
}
}

const result = getSpec(Controller, {specType: SpecTypes.OPENAPI});

expect(result).toEqual({
components: {
schemas: {
Model: {
properties: {
id: {
type: "string"
}
},
type: "object"
},
Model2: {
properties: {
id: {
type: "string"
}
},
type: "object"
}
}
},
paths: {
"/": {
post: {
operationId: "controllerMethod",
parameters: [],
responses: {
"200": {
content: {
"application/json": {
schema: {
oneOf: [
{
$ref: "#/components/schemas/Model"
},
{
$ref: "#/components/schemas/Model2"
}
]
}
}
},
description: "description"
}
},
tags: ["Controller"]
}
}
},
tags: [
{
name: "Controller"
}
]
});
});
});
describe("Multiple contentType", () => {
it("should manage multiple content and model", () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/specs/schema/src/decorators/operations/returns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class ReturnDecoratorContext extends DecoratorContext<ReturnsChainedDecorators>
"location"
];

private hasOfTypes: boolean;

constructor({status, model}: any) {
super();

Expand Down Expand Up @@ -344,7 +346,7 @@ class ReturnDecoratorContext extends DecoratorContext<ReturnsChainedDecorators>
const model = this.get("model");
let contentType = this.get("contentType");

if (model && !isPlainObject(model) && !isPrimitiveOrPrimitiveClass(model)) {
if ((model && !isPlainObject(model) && !isPrimitiveOrPrimitiveClass(model)) || this.hasOfTypes) {
contentType = contentType || "application/json";
}

Expand Down Expand Up @@ -461,6 +463,7 @@ class ReturnDecoratorContext extends DecoratorContext<ReturnsChainedDecorators>

private manyOf(kind: string, types: any[]) {
const model = this.get("model");
this.hasOfTypes = true;

this.addAction(() => {
const schema = this.get("schema") as JsonSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ describe("Discriminator", () => {
responses: {
"200": {
content: {
"*/*": {
"application/json": {
schema: {
discriminator: {
propertyName: "type"
Expand Down Expand Up @@ -1049,7 +1049,7 @@ describe("Discriminator", () => {
responses: {
"200": {
content: {
"*/*": {
"application/json": {
schema: {
discriminator: {
propertyName: "type"
Expand Down Expand Up @@ -1176,7 +1176,7 @@ describe("Discriminator", () => {
responses: {
"200": {
content: {
"*/*": {
"application/json": {
schema: {
discriminator: {
propertyName: "type"
Expand Down Expand Up @@ -1261,7 +1261,7 @@ describe("Discriminator", () => {
responses: {
"200": {
content: {
"*/*": {
"application/json": {
schema: {
$ref: "#/components/schemas/Base"
}
Expand Down

0 comments on commit 0aabf62

Please sign in to comment.