Skip to content

Commit

Permalink
fix(schema): allow declaring Returns.Header response without defined …
Browse files Browse the repository at this point in the history
…value
  • Loading branch information
Romakita committed Mar 26, 2024
1 parent 2cd76dd commit d7b9df2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
53 changes: 53 additions & 0 deletions packages/specs/schema/src/decorators/operations/returns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,59 @@ describe("@Returns", () => {
]
});
});
it("should declare a return type with headers (openspec - header)", () => {
// WHEN
class Controller {
@OperationPath("POST", "/")
@Returns(200, String).Description("description").Header("x-token").Examples({test: "Examples"}).Schema({
minLength: 3
})
method() {}
}

// THEN
const spec = getSpec(Controller, {specType: SpecTypes.OPENAPI});

expect(spec).toEqual({
paths: {
"/": {
post: {
operationId: "controllerMethod",
parameters: [],
responses: {
"200": {
content: {
"*/*": {
examples: {
test: "Examples"
},
schema: {
minLength: 3,
type: "string"
}
}
},
description: "description",
headers: {
"x-token": {
schema: {
type: "string"
}
}
}
}
},
tags: ["Controller"]
}
}
},
tags: [
{
name: "Controller"
}
]
});
});
it("should declare a return type with content-type", () => {
// WHEN
class Controller {
Expand Down
10 changes: 7 additions & 3 deletions packages/specs/schema/src/decorators/operations/returns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface ReturnsChainedDecorators {
* @param key
* @param value
*/
Header(key: string, value: number | string | (JsonHeader & {value?: string | number | boolean})): this;
Header(key: string, value?: number | string | (JsonHeader & {value?: string | number | boolean})): this;

/**
* Add headers
Expand Down Expand Up @@ -227,8 +227,12 @@ class ReturnDecoratorContext extends DecoratorContext<ReturnsChainedDecorators>
return this;
}

header(key: string, value: string | JsonHeader) {
return this.headers({[key]: value});
header(key: string, value?: string | JsonHeader) {
if (value === undefined && isString(key)) {
return this.headers({[key]: {type: "string"}});
}

return this.headers({[key]: value!});
}

location(path: string, meta: JsonHeaders = {}) {
Expand Down

0 comments on commit d7b9df2

Please sign in to comment.