Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): allOf handling #600

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}