RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.37k stars 529 forks source link

Referenced schema could not be loaded when defined in external schema file #1698

Open McMlok opened 3 months ago

McMlok commented 3 months ago

Hello,

I'm consuming an API from a system which define components in multiple schemas/files. The problem is when one type from a schema used for C# client code generation reference type from another schema and this type reference third type which is in second schema. I made simple.

Schema with consuming API

{
    "openapi": "3.0.1",
    "info": {
        "title": "Simple API",
        "version": "1.0"
    },
    "paths": {
        "/api/test": {
            "get": {
                "tags": [],
                "operationId": "GetWeatherForecast",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Foo"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Foo": {
                "type": "object",
                "properties": {
                    "bar": {
                        "$ref": "./referencedSchema.json#/components/schemas/Bar"
                    }
                },
                "additionalProperties": false
            }
        }
    }
}

Second schema saved in same directory in file referencedSchema.json

{
    "openapi": "3.0.1",
    "info": {
        "title": "Common API",
        "version": "1.0"
    },
    "paths": {
       //.......
    },
    "components": {
        "schemas": {
            "Bar": {
                "type": "object",
                "properties": {
                    "language": {
                        "$ref": "#/components/schemas/Language"
                    }
                },
                "additionalProperties": false
            },
            "Language": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "string",
                        "nullable": true
                    }
                },
                "additionalProperties": false
            }
        }
    }
}

An exception throw from Nswag is

System.InvalidOperationException: Error while rendering Liquid template CSharp/Class: The schema reference path '#/components/schemas/Language' has not been resolved. ---> System.InvalidOperationException: The schema reference path '#/components/schemas/Language' has not been resolved.

How to instruct NSwag/NJsonSchema to use all types from all referenced schemas?

Thank you.