docs: merge allOf in OAS to show all object types (#8453)
* docs: merge allOf in OAS to show all object types * add case for non objects
This commit is contained in:
1 parent
8700896ce6
commit
041019b319
2 files changed
+37
-1
No files matched your search
@@ -3,6 +3,7 @@ import dynamic from "next/dynamic"
|
|||||||
import type { TagOperationParametersDefaultProps } from "../Default"
|
import type { TagOperationParametersDefaultProps } from "../Default"
|
||||||
import { TagOperationParametersObjectProps } from "../Object"
|
import { TagOperationParametersObjectProps } from "../Object"
|
||||||
import { Loading } from "docs-ui"
|
import { Loading } from "docs-ui"
|
||||||
|
import mergeAllOfTypes from "../../../../../../utils/merge-all-of-types"
|
||||||
|
|
||||||
const TagOperationParametersObject = dynamic<TagOperationParametersObjectProps>(
|
const TagOperationParametersObject = dynamic<TagOperationParametersObjectProps>(
|
||||||
async () => import("../Object"),
|
async () => import("../Object"),
|
||||||
@@ -34,7 +35,9 @@ const TagOperationParametersUnion = ({
|
|||||||
}: TagOperationParametersUnionProps) => {
|
}: TagOperationParametersUnionProps) => {
|
||||||
const objectSchema = schema.anyOf
|
const objectSchema = schema.anyOf
|
||||||
? schema.anyOf.find((item) => item.type === "object" && item.properties)
|
? schema.anyOf.find((item) => item.type === "object" && item.properties)
|
||||||
: schema.allOf?.find((item) => item.type === "object" && item.properties)
|
: schema.allOf
|
||||||
|
? mergeAllOfTypes(schema)
|
||||||
|
: undefined
|
||||||
|
|
||||||
if (!objectSchema) {
|
if (!objectSchema) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import type { PropertiesObject, SchemaObject } from "@/types/openapi"
|
||||||
|
|
||||||
|
export default function mergeAllOfTypes(
|
||||||
|
allOfSchema: SchemaObject
|
||||||
|
): SchemaObject {
|
||||||
|
if (!allOfSchema.allOf) {
|
||||||
|
// return whatever the schema is
|
||||||
|
return allOfSchema
|
||||||
|
}
|
||||||
|
// merge objects' properties in this var
|
||||||
|
let properties: PropertiesObject = {}
|
||||||
|
let foundObjects = false
|
||||||
|
|
||||||
|
allOfSchema.allOf.forEach((item) => {
|
||||||
|
if (item.type !== "object") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
properties = Object.assign(properties, item.properties)
|
||||||
|
foundObjects = true
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!foundObjects && allOfSchema.allOf.length) {
|
||||||
|
// return the first item in the union.
|
||||||
|
// allOf should always include an object but just in case
|
||||||
|
return allOfSchema.allOf[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: "object",
|
||||||
|
properties,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user