From fe40ce490ff41bb96716be814f05eec7aa9f4336 Mon Sep 17 00:00:00 2001 From: Go Ohtani Date: Wed, 20 Apr 2022 17:45:52 +0900 Subject: [PATCH] fix(app): allOf handling --- .changeset/flat-wasps-report.md | 5 ++ packages/app/src/utils/oas/index.ts | 32 ++++++++- packages/app/static/oas.json | 100 ++++++++++++++++++++++++++++ packages/app/static/shops | 8 +++ 4 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 .changeset/flat-wasps-report.md create mode 100644 packages/app/static/shops diff --git a/.changeset/flat-wasps-report.md b/.changeset/flat-wasps-report.md new file mode 100644 index 000000000..9c40ad6fc --- /dev/null +++ b/.changeset/flat-wasps-report.md @@ -0,0 +1,5 @@ +--- +"@viron/app": patch +--- + +Fix allOf handling. diff --git a/packages/app/src/utils/oas/index.ts b/packages/app/src/utils/oas/index.ts index 80577c884..fa3d3c682 100644 --- a/packages/app/src/utils/oas/index.ts +++ b/packages/app/src/utils/oas/index.ts @@ -89,6 +89,24 @@ export const resolve = (document: Record): Document => { delete result.parent[result.parentProperty].$ref; }, }); + // Merge all schemas defined as `allOf` + JSONPath({ + path: '$..[?(@.allOf)]', + json: document, + resultType: 'all', + callback: (result) => { + result.parent[result.parentProperty] = mergeAllOf(result.value.allOf); + }, + }); + // Clean up all `allOf` properties. + JSONPath({ + path: '$..[?(@.allOf)]', + json: document, + resultType: 'all', + callback: (result) => { + delete result.parent[result.parentProperty].allOf; + }, + }); // Assign contentIds. (document as Document).info['x-pages'].forEach((page) => { page.contents = page.contents.map((content, idx) => { @@ -125,6 +143,7 @@ export const resolve = (document: Record): Document => { }); }); }); + return document as Document; }; @@ -848,5 +867,16 @@ export const getContentBaseOperationResponseKeys = ( }; export const mergeAllOf = (schemas: NonNullable): Schema => { - return _.merge({} as Schema, ...schemas); + const f = (schemas: Schema[]): Schema => { + return _.merge( + {}, + ...schemas.map((schema) => { + if (schema.allOf) { + return f(schema.allOf); + } + return schema; + }) + ); + }; + return f(schemas); }; diff --git a/packages/app/static/oas.json b/packages/app/static/oas.json index c310785e5..7837ff3d3 100644 --- a/packages/app/static/oas.json +++ b/packages/app/static/oas.json @@ -54,6 +54,18 @@ } ] }, + { + "id": "allOf", + "title": "allOf merging", + "description": "# heading 1 \n ## heading 2", + "contents": [ + { + "title": "Shops", + "type": "table", + "operationId": "get:/shops" + } + ] + }, { "id": "pathItem", "title": "PathItem全般", @@ -216,6 +228,24 @@ } } }, + "/shops": { + "description": "summary string.", + "get": { + "operationId": "get:/shops", + "responses": { + "200": { + "description": "allOf merging ", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ShopList" + } + } + } + } + } + } + }, "/pathItem": { "summary": "summary string.", "description": "# heading 1 \n ## heading 2", @@ -875,6 +905,76 @@ }, "components": { "schemas": { + "ShopList": { + "allOf": [ + { + "$ref": "#/components/schemas/ShopPager" + }, + { + "type": "object", + "properties": { + "list": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Shop" + } + } + } + } + ] + }, + "Shop": { + "allOf": [ + { + "$ref": "#/components/schemas/ShopName" + }, + { + "$ref": "#/components/schemas/ShopCategory" + } + ] + }, + "ShopName": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + }, + "ShopCategory": { + "type": "object", + "properties": { + "category": { + "type": "string" + } + } + }, + "ShopPager": { + "allOf": [ + { + "$ref": "#/components/schemas/ShopPagerPage" + }, + { + "$ref": "#/components/schemas/ShopPagerMaxpage" + } + ] + }, + "ShopPagerPage": { + "type": "object", + "properties": { + "page": { + "type": "integer" + } + } + }, + "ShopPagerMaxpage": { + "type": "object", + "properties": { + "maxpage": { + "type" : "integer" + } + } + }, "UserId": { "type": "string" }, diff --git a/packages/app/static/shops b/packages/app/static/shops new file mode 100644 index 000000000..f85cc92ec --- /dev/null +++ b/packages/app/static/shops @@ -0,0 +1,8 @@ +{ + "list": [ + { "name": "shopA", "category": "appliance" }, + { "name": "shopB", "category": "book" } + ], + "page": 1, + "maxpage": 10 +}