2

I am looking to dynamically generate OpenApiSchema from any given Json object in .Net 6. I do not have clr types available in code to autogenerate the Open API Json used by Swashbuckle and need to do so programmatically and not using single object generator tooling. Additionally, these Json objects will change frequently by other users. My objective is to dynamically create an OpenAPI spec from a given json payload. Given a random Json blob like the following, and using C# to generate my own OpenApiSchema similar to this approach https://taerimhan.com/exploring-openapi-extensions-dynamic-schema/. The main difference I'm needing to do is wire up the results directly to OpenAPI so that swashbuckle render's the schmeas and properties for me dynamically. See sample Json...

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

The ideal result would dynamically produce an OpenAPI V3 spec like this...

{
"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
        }
    }
}
}

I've looked at the following resources which are close, but I am looking for a C# solution so that I can use Swashbuckle for lots of different objects of different Json shapes.

Any ideas?

Solo812
  • 401
  • 1
  • 8
  • 19

1 Answers1

0

Unfortunately this is not possible from what I have explored. It is possible to build fragments of an OpenAPI in Swashbuckle, but I cannot see how to apply it in your case.

larsbuch
  • 21
  • 2