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

'Schema reference path has not been resolved' in path referenced yaml file #3175

Open
mwijns opened this issue Nov 17, 2020 · 6 comments
Open

Comments

@mwijns
Copy link

mwijns commented Nov 17, 2020

When generating C# client code with the NSwag command line toolchain 13.9.2 Netcore31 from

root.yaml:

openapi: 3.0.2
paths:
  /test:
    $ref: './path.yaml#/paths/test'

path.yaml:

paths:
  test:
    get:
      requestBody:
        content:
          application/json:
            schema:
              $ref: './object.yaml#/components/schemas/TestRequest'
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: './object.yaml#/components/schemas/TestResponse'

object.yaml

components:
  schemas:
    TestRequest:
      type: object
      properties:
        version:
          type: integer
    TestResponse:
      type: object
      properties:
        version:
          type: integer

I get the following error:

  System.InvalidOperationException: The schema reference path './object.yaml#/components/schemas/TestResponse' has not been resolved.
     at NJsonSchema.JsonSchema.GetActualSchema(IList`1 checkedSchemas)
     at NJsonSchema.JsonSchema.get_ActualSchema()
     at NSwag.CodeGeneration.Models.ResponseModelBase..ctor(IOperationModel operationModel, OpenApiOperation operation, String statusCode, OpenApiResponse response, Boolean isPrimarySuccessResponse, JsonSchema exceptionSchema, TypeResolverBase resolver, CodeGeneratorSettingsBase settings, IClientGenerator generator) in C:\projects\nswag\src\NSwag.CodeGeneration\Models\ResponseModelBase.cs:line 52
     at NSwag.CodeGeneration.CSharp.Models.CSharpResponseModel..ctor(IOperationModel operationModel, OpenApiOperation operation, String statusCode, OpenApiResponse response, Boolean isPrimarySuccessResponse, JsonSchema exceptionSchema, IClientGenerator generator, TypeResolverBase resolver, CodeGeneratorSettingsBase settings) in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\Models\CSharpResponseModel.cs:line 32
     at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel.CreateResponseModel(OpenApiOperation operation, String statusCode, OpenApiResponse response, JsonSchema exceptionSchema, IClientGenerator generator, TypeResolverBase resolver, ClientGeneratorBaseSettings settings) in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\Models\CSharpOperationModel.cs:line 291
     at NSwag.CodeGeneration.Models.OperationModelBase`2.<>c__DisplayClass4_0.<.ctor>b__0(KeyValuePair`2 response) in C:\projects\nswag\src\NSwag.CodeGeneration\Models\OperationModelBase.cs:line 41
     at System.Linq.Enumerable.SelectEnumerableIterator`2.ToList()
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at NSwag.CodeGeneration.Models.OperationModelBase`2..ctor(JsonSchema exceptionSchema, OpenApiOperation operation, TypeResolverBase resolver, IClientGenerator generator, ClientGeneratorBaseSettings settings) in C:\projects\nswag\src\NSwag.CodeGeneration\Models\OperationModelBase.cs:line 40
     at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel..ctor(OpenApiOperation operation, CSharpGeneratorBaseSettings settings, CSharpGeneratorBase generator, CSharpTypeResolver resolver) in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\Models\CSharpOperationModel.cs:line 46
     at NSwag.CodeGeneration.CSharp.CSharpClientGenerator.CreateOperationModel(OpenApiOperation operation, ClientGeneratorBaseSettings settings) in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\CSharpClientGenerator.cs:line 78
     at NSwag.CodeGeneration.ClientGeneratorBase`3.<>c__DisplayClass16_0.<GetOperations>b__1(<>f__AnonymousType0`3 tuple) in C:\projects\nswag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 163
     at System.Linq.Enumerable.SelectEnumerableIterator`2.ToList()
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at NSwag.CodeGeneration.ClientGeneratorBase`3.GetOperations(OpenApiDocument document) in C:\projects\nswag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 152
     at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateAllClientTypes() in C:\projects\nswag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 106
     at NSwag.CodeGeneration.ClientGeneratorBase`3.GenerateFile(ClientGeneratorOutputType outputType) in C:\projects\nswag\src\NSwag.CodeGeneration\ClientGeneratorBase.cs:line 73
     at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.GenerateContracts(Dictionary`2 result, CSharpClientGenerator clientGenerator) in C:\projects\nswag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 286
     at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<RunAsync>b__92_0() in C:\projects\nswag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 260
     at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.RunAsync() in C:\projects\nswag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 251
     at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\CodeGeneration\OpenApiToCSharpClientCommand.cs:line 240
     at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 85
     at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 86
     at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 32
     at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
     at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
     at NConsole.CommandLineProcessor.Process(String[] args, Object input)
     at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 56

I saw that path reference support was added recently (#1953). When recreating the same case in ExternalReferenceTests.cs / path-reference.json no error is given.
When generating from the same files with the 13.6.0 toolchain no error is given and the code is correctly generated.

@goldsam
Copy link
Contributor

goldsam commented Feb 22, 2021

Any update to this?

@mwijns
Copy link
Author

mwijns commented Feb 23, 2021

I stuck to the 13.6.x version and haven't tried it with a more recent version lately.

@RicoSuter
Copy link
Owner

RicoSuter commented Feb 23, 2021

I have the feeling this might have been fixed with this PR:
RicoSuter/NJsonSchema#1317

Please try again with v13.10.4+

@roelofvdv
Copy link

I get the exact same issue when trying to generate a C# client based on a public Swagger 2.0 yaml specification.
This is the spec: https://dev.float.com/swagger-api-v3.yaml

I'm using version 13.11.3 of the Nuget packages NSwag.CodeGeneration.CSharp and NSwag.Core.Yaml

    static async Task Main(string[] args)
    {
        const string endpoint = "https://dev.float.com/swagger-api-v3.yaml";

        var document = await OpenApiYamlDocument.FromUrlAsync(endpoint);
        var settings = new CSharpClientGeneratorSettings
        {
            ClassName = "MyClass",
            CSharpGeneratorSettings =
            {
                Namespace = "MyNamespace",
            }
        };

        var generator = new CSharpClientGenerator(document, settings);
        var code = generator.GenerateFile();
    }

Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in NJsonSchema.dll but was not handled in user code: 'The schema reference path '#/definitions/ReportsPeople' has not been resolved.'
at NJsonSchema.JsonSchema.GetActualSchema(IList1 checkedSchemas) at NJsonSchema.JsonSchema.get_ActualSchema() at NSwag.OpenApiDocument.<>c.<GenerateOperationIds>b__63_3(KeyValuePair2 r)
at System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func2 predicate)
at NSwag.OpenApiDocument.<>c.b__63_2(OpenApiOperationDescription o)
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Func2 predicate, Boolean& found)
at NSwag.OpenApiDocument.GenerateOperationIds()
at NSwag.CodeGeneration.ClientGeneratorBase3.GetOperations(OpenApiDocument document) at NSwag.CodeGeneration.ClientGeneratorBase3.GenerateAllClientTypes()
at NSwag.CodeGeneration.ClientGeneratorBase3.GenerateFile(ClientGeneratorOutputType outputType) at NSwag.CodeGeneration.ClientGeneratorBase3.GenerateFile()
at NSwagClientGenerator.Program.

d__0.MoveNext() in C:\Users\Roelof\source\repos\fiddles\NSwagClientGenerator\Program.cs:line 26

Any help would be appreciated

@VusalDev
Copy link

Same issue with 13.20.0.
Generate client/controller from someapi.yaml, which $ref schema from ./includes/common.yaml, which $ref schema from itself.

System.InvalidOperationException: The schema reference path './common.yaml#/components/schemas/NanoId' has not been resolved.

@xamele0n
Copy link

xamele0n commented Jul 3, 2024

I`ve found that external references works only for section #/definitions
If you have document A.yaml referencing document B.yaml it loads B as JsonSchema document (not OpenAPI document). Then JsonSchema search references in /definitions section.

First reference from A to B works well, but all other references in B must point to #/definitions section (https://json-schema.org/draft-07/schema#)

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

6 participants