Skip to content

Commit

Permalink
fix: Open API spec, array relation and inverse relation should be man…
Browse files Browse the repository at this point in the history
…ually set readonly/writeonly (#983)
  • Loading branch information
ktutnik committed Jun 23, 2021
1 parent d2de509 commit 7684287
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 266 deletions.
5 changes: 0 additions & 5 deletions packages/swagger/src/transform/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ function transformModel(model: ParameterNode, ctx: TransformContext) {
// if not partial validator then add required override
if (!model.meta.decorators.find(isPartialValidator))
overrides.push("Required")
// if contains @api.noRelation() then adds remove all relations
if (!!model.meta.decorators.find((x: ApiHideRelationDecorator) => x.kind === "ApiNoRelation")) {
overrides.push("RemoveInverseProperty")
overrides.push("RemoveArrayRelation")
}
overrides.push("ReadonlyFields")
return transformTypeAdvance(model.type, ctx, { decorators: model.meta.decorators, overrides })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,66 +921,6 @@ Object {
}
`;

exports[`Open Api Generic Many To One Controller Should hide relation on POST method 1`] = `
Object {
"content": Object {
"application/json": Object {
"schema": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Tag",
},
Object {
"properties": Object {
"animal": Object {
"type": "number",
},
},
"type": "object",
},
Object {
"properties": Object {
"animal": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
},
"application/x-www-form-urlencoded": Object {
"schema": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Tag",
},
Object {
"properties": Object {
"animal": Object {
"type": "number",
},
},
"type": "object",
},
Object {
"properties": Object {
"animal": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
},
},
"required": true,
}
`;

exports[`Open Api Generic One To Many Controller Should able to provide correct parameter name when using custom path name 1`] = `
Array [
Object {
Expand Down Expand Up @@ -1338,15 +1278,6 @@ Object {
},
"type": "object",
},
Object {
"properties": Object {
"tags": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
},
Expand All @@ -1367,15 +1298,6 @@ Object {
},
"type": "object",
},
Object {
"properties": Object {
"tags": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
},
Expand Down
25 changes: 0 additions & 25 deletions tests/behavior/generic-controller/generic-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1634,31 +1634,6 @@ describe("Open Api", () => {
})

describe("Generic Many To One Controller", () => {
it("Should hide relation on POST method", async () => {
@genericController()
class Animal {
@entity.primaryId()
id: number
@reflect.noop()
name: string
}
class Tag {
@entity.primaryId()
id: number
@reflect.noop()
tag: string
@genericController()
@entity.relation()
animal:Animal
}
const koa = await createApp({ controller: Tag }, { mode: "production" })
.set(new SwaggerFacility())
.initialize()
const { body } = await supertest(koa.callback())
.get("/swagger/swagger.json")
.expect(200)
expect(body.paths["/animal/{pid}/tag"].post.requestBody).toMatchSnapshot()
})
it("Should hide relation on GET all response", async () => {
@genericController()
class Animal {
Expand Down
96 changes: 0 additions & 96 deletions tests/behavior/swagger/__snapshots__/open-api.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2603,102 +2603,6 @@ Object {
}
`;

exports[`Open API 3.0 Generation Schema Override Request Body Should hide reverse relation 1`] = `
Object {
"content": Object {
"application/json": Object {
"schema": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Item",
},
Object {
"properties": Object {
"categories": Object {
"items": Object {
"type": "number",
},
"type": "array",
},
"createdBy": Object {
"type": "number",
},
"shop": Object {
"type": "number",
},
},
"type": "object",
},
Object {
"properties": Object {
"shop": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
Object {
"properties": Object {
"categories": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
},
"application/x-www-form-urlencoded": Object {
"schema": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Item",
},
Object {
"properties": Object {
"categories": Object {
"items": Object {
"type": "number",
},
"type": "array",
},
"createdBy": Object {
"type": "number",
},
"shop": Object {
"type": "number",
},
},
"type": "object",
},
Object {
"properties": Object {
"shop": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
Object {
"properties": Object {
"categories": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
},
},
"required": true,
}
`;

exports[`Open API 3.0 Generation Schema Override Request Body Should ignore required property when partial validation applied 1`] = `
Object {
"content": Object {
Expand Down
62 changes: 0 additions & 62 deletions tests/behavior/swagger/open-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,68 +185,6 @@ describe("Open API 3.0 Generation", () => {
.expect(200)
expect(body.paths["/item"].post.requestBody).toMatchSnapshot()
})
it("Should hide reverse relation", async () => {
class Shop {
@entity.primaryId()
id: number

@noop()
name: string

@entity.relation({ inverseProperty: "shop" })
@type(x => [Item])
items: Item[]
}
class User {
@entity.primaryId()
id: number

@noop()
name: string

@entity.relation()
@type(x => [Shop])
shops: Shop[]
}
class Category {
@entity.primaryId()
id: number

@noop()
name: string
}
class Item {
@entity.primaryId()
id: number

@noop()
name: string

@noop()
price: number

@entity.relation()
shop: Shop

@entity.relation()
@type(x => [Category])
categories: Category[]

@entity.relation()
createdBy: User
}
@generic.argument(Shop, Item, Number, Number)
@decorateClass(<NestedGenericControllerDecorator>{ kind: "plumier-meta:relation-prop-name", relation: "items", type: Shop })
class ItemController {
@route.post("")
save(@api.hideRelations() data: Item) { }
}
const app = await createApp(ItemController)
const { body } = await supertest(app.callback())
.get("/swagger/swagger.json")
.expect(200)
expect(body.paths["/item"].post.requestBody).toMatchSnapshot()
})
it("Should respect readonly property", async () => {
class Shop {
@entity.primaryId()
Expand Down
64 changes: 64 additions & 0 deletions tests/behavior/typeorm/__snapshots__/typeorm-generic.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,70 @@ Object {
}
`;

exports[`Open API Should reflect relation on many to many column 1`] = `
Object {
"content": Object {
"application/json": Object {
"schema": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/User",
},
Object {
"properties": Object {
"animals": Object {
"items": Object {
"type": "number",
},
"type": "array",
},
},
"type": "object",
},
Object {
"properties": Object {
"id": Object {
"readOnly": true,
},
},
"type": "object",
},
],
},
},
"application/x-www-form-urlencoded": Object {
"schema": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/User",
},
Object {
"properties": Object {
"animals": Object {
"items": Object {
"type": "number",
},
"type": "array",
},
},
"type": "object",
},
Object {
"properties": Object {
"id": Object {
"readOnly": true,
},
},
"type": "object",
},
],
},
},
},
"required": true,
}
`;

exports[`Repository Repository Should able to get one 1`] = `
User {
"email": "john.doe@gmail.com",
Expand Down

0 comments on commit 7684287

Please sign in to comment.