Skip to content

Commit

Permalink
fix(schema): add support JsonSchema to In.Schema decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jun 1, 2024
1 parent 540370b commit a7387e7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
94 changes: 92 additions & 2 deletions packages/specs/schema/src/decorators/operations/in.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {execMapper, getSpec, In, JsonEntityStore, Name, OperationPath, Path, SpecTypes} from "../../index";
import {execMapper, getSpec, In, JsonEntityStore, Name, number, OperationPath, Path, SpecTypes} from "../../index";

describe("In", () => {
it("should declare all schema correctly (param)", () => {
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("In", () => {
}
});
});
it("should extra schema", () => {
it("should declare a pattern", () => {
// WHEN
@Path("/:parentId")
@In("path")
Expand Down Expand Up @@ -213,6 +213,96 @@ describe("In", () => {
]
});
});
it("should declare schema", () => {
// WHEN
@Path("/:parentId")
@In("path").Name("parentId").Required().Description("description").Schema(number().integer().minimum(2))
class Controller {
@OperationPath("GET", "/:path")
method(@In("path") @Name("basic") basic: string) {}
}

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

const paramSchema = JsonEntityStore.from(Controller, "method", 0);
const methodSchema = paramSchema.parent;
const operation = execMapper("operation", [methodSchema.operation!], {
specType: SpecTypes.OPENAPI
});

expect(operation).toEqual({
parameters: [
{
in: "path",
name: "basic",
required: true,
schema: {
type: "string"
}
},
{
description: "description",
in: "path",
name: "parentId",
required: true,
schema: {
minimum: 2,
multipleOf: 1,
type: "integer"
}
}
],
responses: {
"200": {
description: "Success"
}
}
});
expect(spec).toEqual({
paths: {
"/{parentId}/{path}": {
get: {
operationId: "controllerMethod",
parameters: [
{
description: "description",
in: "path",
name: "parentId",
required: true,
schema: {
minimum: 2,
multipleOf: 1,
type: "integer"
}
},
{
in: "path",
name: "path",
required: true,
schema: {
type: "string"
}
}
],
responses: {
"200": {
description: "Success"
}
},
tags: ["Controller"]
}
}
},
tags: [
{
name: "Controller"
}
]
});
});
it("should throw error for unsupported usage", () => {
class Test {
test() {}
Expand Down
4 changes: 2 additions & 2 deletions packages/specs/schema/src/decorators/operations/in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface InChainedDecorators {
* Add custom schema.
* @param schema
*/
Schema(schema: Partial<JsonSchemaObject>): this;
Schema(schema: Partial<JsonSchemaObject | JsonSchema>): this;
}

/**
Expand Down Expand Up @@ -131,7 +131,7 @@ export function In(inType: JsonParameterTypes | string): InChainedDecorators {
};

decorator.Schema = (_schema: any) => {
Object.assign(schema, _schema);
Object.assign(schema, _schema.toJSON ? _schema.toJSON() : _schema);

return decorator;
};
Expand Down

0 comments on commit a7387e7

Please sign in to comment.