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

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

Open
McMlok opened this issue May 2, 2024 · 0 comments
Open

Comments

@McMlok
Copy link

McMlok commented May 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant