swagger 从动态Json对象动态生成C#中的OpenApiSchema

huwehgph  于 2022-11-06  发布在  C#
关注(0)|答案(1)|浏览(246)

我希望从. Net6中的任何给定Json对象动态生成OpenApiSchema。我在代码中没有可用的clr类型来自动生成Swashbuckle所使用的OpenAPIJson,需要通过编程来完成,而不是使用单个对象生成器工具。此外,这些Json对象将被其他用户频繁地修改。2我的目标是从一个给定的Json负载动态地创建一个OpenAPI规范。给定一个如下所示的随机Json blob,使用C#生成我自己的OpenApiSchema,类似于https://taerimhan.com/exploring-openapi-extensions-dynamic-schema/方法。我需要做的主要区别是将结果直接连接到OpenAPI,以便swashbuckle动态地为我呈现schmeas和属性。请参见示例Json...

{
    "id": "123",
    "unassumablepropertyname1": {
        "id": "456",
        "otherprop": null
    },
    "dynamicproperty": "456",
    "somethinguseful": "ABC123"
}

理想的结果是动态地生成一个OpenAPI V3规范,如下所示...

{
"openapi": "3.0.1",
"info": {
    "title": "Sample API Capabilities",
    "version": "v1"
},
"paths": {
    "/api/DataRecord/UpsertDynamicObj12": {
        "post": {
            "tags": [
                "Dynamic Objects"
            ],
            "operationId": "UpsertDynamicExample",
            "requestBody": {
                "content": {
                    "application/json": {
                        "schema": {
                            "$ref": "#/components/schemas/DynamicObj12"
                        }
                    },
                    "text/json": {
                        "schema": {
                            "$ref": "#/components/schemas/DynamicObj12"
                        }
                    },
                    "application/*+json": {
                        "schema": {
                            "$ref": "#/components/schemas/DynamicObj12"
                        }
                    }
                }
            },
            "responses": {
                "200": {
                    "description": "Success"
                }
            }
        }
    }
},
"components": {
    "schemas": {
        "DynamicObj12": {
            "required": [
                "id"
            ],
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "dynamicproperty": {
                    "type": "string"
                },
                "somethinguseful": {
                    "type": "string"
                },
                "unassumablepropertyname1": {
                    "type": "array",
                    "items": {
                        "$ref": "#/components/schemas/UnassumablePropertyName1"
                    },
                    "nullable": true
                }
            },
            "additionalProperties": false
        },
        "UnassumablePropertyName1": {
            "required": [
                "id"
            ],
            "type": "object",
            "properties": {
                "id": {
                    "type": "string",
                    "format": "uuid"
                },
                "otherprop": {
                    "type": "string",
                    "format": "uuid"
                }
            },
            "additionalProperties": false
        }
    }
}
}

我已经看了下面的资源,它们很接近,但是我正在寻找一个C#解决方案,这样我就可以将Swashbuckle用于许多不同Json形状的不同对象。

有什么想法吗?

6qfn3psc

6qfn3psc1#

不幸的是,从我所探索的内容来看,这是不可能的。在Swashbuckle中构建OpenAPI的片段是可能的,但我不知道如何将其应用到您的案例中。

相关问题