From 13d39147cb6df32a7404f089329a1905b5eca4be Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Thu, 14 Nov 2024 12:29:05 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Don=E2=80=99t=20expose=20FKs=20as=20attribu?= =?UTF-8?q?tes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/openapi/src/rest-generator.ts | 4 +++ .../tests/baseline/rest-3.0.0.baseline.yaml | 29 ----------------- .../tests/baseline/rest-3.1.0.baseline.yaml | 32 ------------------- 3 files changed, 4 insertions(+), 61 deletions(-) diff --git a/packages/plugins/openapi/src/rest-generator.ts b/packages/plugins/openapi/src/rest-generator.ts index 4e7c8bcb..a38b1233 100644 --- a/packages/plugins/openapi/src/rest-generator.ts +++ b/packages/plugins/openapi/src/rest-generator.ts @@ -868,6 +868,10 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase { const required: string[] = []; for (const field of fields) { + if (isForeignKeyField(field)) { + // foreign keys are not exposed as attributes + continue; + } if (isRelationshipField(field)) { let relType: string; if (mode === 'create' || mode === 'update') { diff --git a/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml index adb9ded1..aa5c7536 100644 --- a/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml @@ -2764,11 +2764,8 @@ components: image: type: string nullable: true - userId: - type: string required: - image - - userId relationships: type: object properties: @@ -2791,14 +2788,10 @@ components: type: string attributes: type: object - required: - - userId properties: image: type: string nullable: true - userId: - type: string relationships: type: object properties: @@ -2830,8 +2823,6 @@ components: image: type: string nullable: true - userId: - type: string relationships: type: object properties: @@ -2917,9 +2908,6 @@ components: format: date-time title: type: string - authorId: - type: string - nullable: true published: type: boolean viewCount: @@ -2931,7 +2919,6 @@ components: - createdAt - updatedAt - title - - authorId - published - viewCount - notes @@ -2974,9 +2961,6 @@ components: format: date-time title: type: string - authorId: - type: string - nullable: true published: type: boolean viewCount: @@ -3022,9 +3006,6 @@ components: format: date-time title: type: string - authorId: - type: string - nullable: true published: type: boolean viewCount: @@ -3108,16 +3089,6 @@ components: type: string type: type: string - attributes: - type: object - properties: - postId: - type: string - userId: - type: string - required: - - postId - - userId relationships: type: object properties: diff --git a/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml index f69536b3..58ca6b91 100644 --- a/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml @@ -2770,11 +2770,8 @@ components: oneOf: - type: 'null' - type: string - userId: - type: string required: - image - - userId relationships: type: object properties: @@ -2797,15 +2794,11 @@ components: type: string attributes: type: object - required: - - userId properties: image: oneOf: - type: 'null' - type: string - userId: - type: string relationships: type: object properties: @@ -2838,8 +2831,6 @@ components: oneOf: - type: 'null' - type: string - userId: - type: string relationships: type: object properties: @@ -2925,10 +2916,6 @@ components: format: date-time title: type: string - authorId: - oneOf: - - type: 'null' - - type: string published: type: boolean viewCount: @@ -2941,7 +2928,6 @@ components: - createdAt - updatedAt - title - - authorId - published - viewCount - notes @@ -2984,10 +2970,6 @@ components: format: date-time title: type: string - authorId: - oneOf: - - type: 'null' - - type: string published: type: boolean viewCount: @@ -3034,10 +3016,6 @@ components: format: date-time title: type: string - authorId: - oneOf: - - type: 'null' - - type: string published: type: boolean viewCount: @@ -3122,16 +3100,6 @@ components: type: string type: type: string - attributes: - type: object - properties: - postId: - type: string - userId: - type: string - required: - - postId - - userId relationships: type: object properties: From 1e4f9c173b46ceaa9c04d71f6c798269d322e5ae Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Fri, 15 Nov 2024 10:52:21 +0100 Subject: [PATCH 2/3] Only skip foreign keys for writes --- packages/plugins/openapi/src/rest-generator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugins/openapi/src/rest-generator.ts b/packages/plugins/openapi/src/rest-generator.ts index a38b1233..86ed9736 100644 --- a/packages/plugins/openapi/src/rest-generator.ts +++ b/packages/plugins/openapi/src/rest-generator.ts @@ -868,7 +868,7 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase { const required: string[] = []; for (const field of fields) { - if (isForeignKeyField(field)) { + if (isForeignKeyField(field) && mode !== 'read') { // foreign keys are not exposed as attributes continue; } From c9fffac2de36921539d59fe93f1db1b55bd2539f Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Fri, 15 Nov 2024 11:40:38 +0100 Subject: [PATCH 3/3] Add back read attrs to baselines --- .../tests/baseline/rest-3.0.0.baseline.yaml | 17 +++++++++++++++++ .../tests/baseline/rest-3.1.0.baseline.yaml | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml index aa5c7536..0ea25801 100644 --- a/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml @@ -2764,8 +2764,11 @@ components: image: type: string nullable: true + userId: + type: string required: - image + - userId relationships: type: object properties: @@ -2908,6 +2911,9 @@ components: format: date-time title: type: string + authorId: + type: string + nullable: true published: type: boolean viewCount: @@ -2919,6 +2925,7 @@ components: - createdAt - updatedAt - title + - authorId - published - viewCount - notes @@ -3089,6 +3096,16 @@ components: type: string type: type: string + attributes: + type: object + properties: + postId: + type: string + userId: + type: string + required: + - postId + - userId relationships: type: object properties: diff --git a/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml b/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml index 58ca6b91..9bd34467 100644 --- a/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml +++ b/packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml @@ -2770,8 +2770,11 @@ components: oneOf: - type: 'null' - type: string + userId: + type: string required: - image + - userId relationships: type: object properties: @@ -2916,6 +2919,10 @@ components: format: date-time title: type: string + authorId: + oneOf: + - type: 'null' + - type: string published: type: boolean viewCount: @@ -2928,6 +2935,7 @@ components: - createdAt - updatedAt - title + - authorId - published - viewCount - notes @@ -3100,6 +3108,16 @@ components: type: string type: type: string + attributes: + type: object + properties: + postId: + type: string + userId: + type: string + required: + - postId + - userId relationships: type: object properties: