NJsonSchema icon indicating copy to clipboard operation
NJsonSchema copied to clipboard

Unable to cast object of type 'NSwag.OpenApiDocument' to type 'NJsonSchema.JsonSchema'

Open brendonparker opened this issue 2 years ago • 1 comments

I'm sure this is a dumb question. I have an openapi spec provided to me. I want to generate the CSharp objects. Trying this:

using NJsonSchema.CodeGeneration.CSharp;

var openapi = await NSwag.OpenApiDocument.FromFileAsync("spec.json");
var generator = new CSharpGenerator(openapi);
var file = generator.GenerateFile();

Console.WriteLine(file);

But getting:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'NSwag.OpenApiDocument' to type 'NJsonSchema.JsonSchema'.
   at NJsonSchema.CodeGeneration.GeneratorBase.GenerateFile()

The Readme seems to indicate that this is supported (openapi), but what is the recipe to make this happen?

net8.0 with the following packages:

<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="11.0.0" />
<PackageReference Include="NSwag.Core" Version="14.0.0" />

brendonparker avatar Jan 11 '24 01:01 brendonparker

I would read it as a JsonSchema:

 var schema = await JsonSchema.FromFileAsync( "spec.json" );

Purgator avatar Feb 09 '24 16:02 Purgator