Skip to content

Commit

Permalink
Merge pull request #600 from cam-inc/fix_allof
Browse files Browse the repository at this point in the history
fix(app): allOf handling
  • Loading branch information
cathcheeno authored Apr 20, 2022
2 parents 4e03b0f + fe40ce4 commit 59a677f
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/flat-wasps-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@viron/app": patch
---

Fix allOf handling.
32 changes: 31 additions & 1 deletion packages/app/src/utils/oas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ export const resolve = (document: Record<string, unknown>): 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) => {
Expand Down Expand Up @@ -125,6 +143,7 @@ export const resolve = (document: Record<string, unknown>): Document => {
});
});
});

return document as Document;
};

Expand Down Expand Up @@ -848,5 +867,16 @@ export const getContentBaseOperationResponseKeys = (
};

export const mergeAllOf = (schemas: NonNullable<Schema['allOf']>): 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);
};
100 changes: 100 additions & 0 deletions packages/app/static/oas.json
Original file line number Diff line number Diff line change
Expand Up @@ -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全般",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/app/static/shops
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"list": [
{ "name": "shopA", "category": "appliance" },
{ "name": "shopB", "category": "book" }
],
"page": 1,
"maxpage": 10
}

0 comments on commit 59a677f

Please sign in to comment.