Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): Fix ReadOnly decorator used with $ref. #1607

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 130 additions & 1 deletion packages/orm/mongoose/src/decorators/virtualRef.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {getJsonSchema, Property} from "@tsed/schema";
import {Default, Format, getJsonSchema, getSpec, Post, Property, ReadOnly, Returns, SpecTypes} from "@tsed/schema";
import {Store} from "@tsed/core";
import {Model} from "@tsed/mongoose";
import {MONGOOSE_SCHEMA} from "../constants";
import {VirtualRef} from "./virtualRef";
import {Controller} from "@tsed/di";
import {BodyParams} from "@tsed/platform-params";

describe("@VirtualRef()", () => {
describe("when type and foreign value are given", () => {
Expand Down Expand Up @@ -131,5 +133,132 @@ describe("@VirtualRef()", () => {
type: "object"
});
});
it("should generate spec", () => {
@Model({name: "VirtualTestPerson"})
class TestPerson {
@Property()
name: string;

@Property()
band: string;
}

// WHEN
@Model({name: "VirtualTestBand"})
class TestBand {
@Format("date-time")
@ReadOnly()
createdAt: number = Date.now();

@Format("date-time")
@ReadOnly()
updatedAt: number = Date.now();

@ReadOnly()
@VirtualRef({
ref: TestPerson,
foreignField: "foreign",
localField: "test_2",
justOne: true,
options: {}
})
members: VirtualRef<TestPerson>;
}

@Controller("/")
class MyCtrl {
@Post("/")
@Returns(200, TestBand)
post(@BodyParams() model: TestBand) {}
}

// THEN
const store = Store.from(TestBand, "members");

expect(store.get(MONGOOSE_SCHEMA)).toEqual({
ref: "VirtualTestPerson",
localField: "test_2",
foreignField: "foreign",
justOne: true,
options: {}
});

expect(getSpec(MyCtrl, {specType: SpecTypes.OPENAPI})).toEqual({
components: {
schemas: {
TestBand: {
properties: {
createdAt: {
format: "date-time",
readOnly: true,
type: "number"
},
members: {
allOf: [
{
$ref: "#/components/schemas/TestPerson"
}
],
readOnly: true
},
updatedAt: {
format: "date-time",
readOnly: true,
type: "number"
}
},
type: "object"
},
TestPerson: {
properties: {
band: {
type: "string"
},
name: {
type: "string"
}
},
type: "object"
}
}
},
paths: {
"/": {
post: {
operationId: "myCtrlPost",
parameters: [],
requestBody: {
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/TestBand"
}
}
},
required: false
},
responses: {
"200": {
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/TestBand"
}
}
},
description: "Success"
}
},
tags: ["MyCtrl"]
}
}
},
tags: [
{
name: "MyCtrl"
}
]
});
});
});
});
215 changes: 214 additions & 1 deletion packages/specs/schema/src/decorators/common/readOnly.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {getJsonSchema} from "@tsed/schema";
import {Format, getJsonSchema, getSpec, Post, Property, Returns, SpecTypes} from "@tsed/schema";
import {expect} from "chai";
import {ReadOnly} from "./readOnly";
import {Controller} from "@tsed/di";
import {BodyParams} from "@tsed/platform-params";

describe("@ReadOnly", () => {
it("should declare readOnly field", () => {
Expand All @@ -23,4 +25,215 @@ describe("@ReadOnly", () => {
type: "object"
});
});

it("should declare property as readonly (OS3)", () => {
class TestPerson {
@Property()
name: string;

@Property()
band: string;
}

// WHEN
class TestBand {
@Format("date-time")
@ReadOnly()
createdAt: number = Date.now();

@Format("date-time")
@ReadOnly()
updatedAt: number = Date.now();

@ReadOnly()
@Property(TestPerson)
members: TestPerson;
}

@Controller("/")
class MyCtrl {
@Post("/")
@Returns(200, TestBand)
post(@BodyParams() model: TestBand) {}
}

// THEN
expect(getSpec(MyCtrl, {specType: SpecTypes.OPENAPI})).to.deep.equal({
components: {
schemas: {
TestBand: {
properties: {
createdAt: {
format: "date-time",
readOnly: true,
type: "number"
},
members: {
allOf: [
{
$ref: "#/components/schemas/TestPerson"
}
],
readOnly: true
},
updatedAt: {
format: "date-time",
readOnly: true,
type: "number"
}
},
type: "object"
},
TestPerson: {
properties: {
band: {
type: "string"
},
name: {
type: "string"
}
},
type: "object"
}
}
},
paths: {
"/": {
post: {
operationId: "myCtrlPost",
parameters: [],
requestBody: {
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/TestBand"
}
}
},
required: false
},
responses: {
"200": {
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/TestBand"
}
}
},
description: "Success"
}
},
tags: ["MyCtrl"]
}
}
},
tags: [
{
name: "MyCtrl"
}
]
});
});
it("should declare property as readonly (OS2)", () => {
class TestPerson {
@Property()
name: string;

@Property()
band: string;
}

// WHEN
class TestBand {
@Format("date-time")
@ReadOnly()
createdAt: number = Date.now();

@Format("date-time")
@ReadOnly()
updatedAt: number = Date.now();

@ReadOnly()
@Property(TestPerson)
members: TestPerson;
}

@Controller("/")
class MyCtrl {
@Post("/")
@Returns(200, TestBand)
post(@BodyParams() model: TestBand) {}
}

// THEN
expect(getSpec(MyCtrl, {specType: SpecTypes.SWAGGER})).to.deep.equal({
definitions: {
TestBand: {
properties: {
createdAt: {
format: "date-time",
type: "number"
},
members: {
allOf: [
{
$ref: "#/definitions/TestPerson"
}
],
readOnly: true
},
updatedAt: {
format: "date-time",
type: "number"
}
},
type: "object"
},
TestPerson: {
properties: {
band: {
type: "string"
},
name: {
type: "string"
}
},
type: "object"
}
},
paths: {
"/": {
post: {
operationId: "myCtrlPost",
parameters: [
{
in: "body",
name: "body",
required: false,
schema: {
$ref: "#/definitions/TestBand"
}
}
],
produces: ["application/json"],
responses: {
"200": {
description: "Success",
schema: {
$ref: "#/definitions/TestBand"
}
}
},
tags: ["MyCtrl"]
}
}
},
tags: [
{
name: "MyCtrl"
}
]
});
});
});