Skip to content

Commit

Permalink
fix(swagger): Hide grand children relation properties on Swagger resp…
Browse files Browse the repository at this point in the history
…onse example (#1012)
  • Loading branch information
ktutnik committed Jul 18, 2021
1 parent 13992cf commit f153bed
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/swagger/src/transform/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FormFile,
RelationDecorator,
NestedGenericControllerDecorator,
isCustomClass,
} from "@plumier/core"
import reflect, { IsDynamicType } from "@plumier/reflect"
import { ReferenceObject, SchemaObject } from "openapi3-ts"
Expand Down Expand Up @@ -141,7 +142,7 @@ function removeChildRelationsOverride(modelType: (Class | Class[]), ctx: Transfo
const result: SchemaObject = { type: "object", properties: {} }
for (const property of meta.properties) {
const relation = property.decorators.find(isRelation)
if (relation && property.typeClassification === "Class") {
if (relation && isCustomClass(property.type)) {
const childMeta = getMetadata(property.type as Class)
const childSchema: SchemaObject = { type: "object", properties: {} }
for (const prop of childMeta.properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,35 @@ Object {
"application/json": Object {
"schema": Object {
"items": Object {
"$ref": "#/components/schemas/UserTrans",
"allOf": Array [
Object {
"$ref": "#/components/schemas/UserTrans",
},
Object {
"properties": Object {
"todos": Object {
"items": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Todo",
},
Object {
"properties": Object {
"user": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
"type": "array",
},
},
"type": "object",
},
],
},
"type": "array",
},
Expand All @@ -1685,7 +1713,35 @@ Object {
"content": Object {
"application/json": Object {
"schema": Object {
"$ref": "#/components/schemas/UserTrans",
"allOf": Array [
Object {
"$ref": "#/components/schemas/UserTrans",
},
Object {
"properties": Object {
"todos": Object {
"items": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Todo",
},
Object {
"properties": Object {
"user": Object {
"readOnly": true,
"writeOnly": true,
},
},
"type": "object",
},
],
},
"type": "array",
},
},
"type": "object",
},
],
},
},
},
Expand Down
30 changes: 29 additions & 1 deletion tests/behavior/swagger/__snapshots__/open-api.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,7 @@ Object {
}
`;

exports[`Open API 3.0 Generation Schema Override Response Body Should hide all child relations 1`] = `
exports[`Open API 3.0 Generation Schema Override Response Body Should hide all grand child relations 1`] = `
Object {
"200": Object {
"content": Object {
Expand All @@ -2709,6 +2709,20 @@ Object {
},
Object {
"properties": Object {
"categories": Object {
"items": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Category",
},
Object {
"properties": Object {},
"type": "object",
},
],
},
"type": "array",
},
"createdBy": Object {
"allOf": Array [
Object {
Expand Down Expand Up @@ -2765,6 +2779,20 @@ Object {
},
Object {
"properties": Object {
"categories": Object {
"items": Object {
"allOf": Array [
Object {
"$ref": "#/components/schemas/Category",
},
Object {
"properties": Object {},
"type": "object",
},
],
},
"type": "array",
},
"createdBy": Object {
"allOf": Array [
Object {
Expand Down
6 changes: 4 additions & 2 deletions tests/behavior/swagger/open-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ describe("Open API 3.0 Generation", () => {
})
})
describe("Response Body", () => {

it("Should hide all child relations", async () => {
it("Should hide all grand child relations", async () => {
class Shop {
@entity.primaryId()
id: number
Expand Down Expand Up @@ -320,6 +319,9 @@ describe("Open API 3.0 Generation", () => {

@noop()
name: string

@noop()
createdBy:User
}
class Item {
@entity.primaryId()
Expand Down

0 comments on commit f153bed

Please sign in to comment.