RicoSuter / NJsonSchema

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

Typescript generation crashes with stack overflow error #1246

Open ka-ramba opened 4 years ago

ka-ramba commented 4 years ago

The attached json document causes stack overflow in tscript generator if Generate DTO is set to true. minimal.zip

RicoSuter commented 4 years ago

This is bad and must be fixed in NJsonSchema.. i suppose you dont have a stacktrace?

ka-ramba commented 4 years ago

The stacktrace at the time of failure is below. However I believe the actual error is in NJsonSchema.CodeGeneration.TypeScript.GenerateTypes(...) which I assume produces an object with circular dependencies.

> NSwag.CodeGeneration.TypeScript.dll!NSwag.CodeGeneration.TypeScript.Models.TypeScriptFileTemplateModel.TypeScriptFileTemplateModel(System.Collections.Generic.IEnumerable<NJsonSchema.CodeGeneration.CodeArtifact> clientTypes, System.Collections.Generic.IEnumerable<NJsonSchema.CodeGeneration.CodeArtifact> dtoTypes, NSwag.OpenApiDocument document, NJsonSchema.CodeGeneration.TypeScript.TypeScriptExtensionCode extensionCode, NSwag.CodeGeneration.TypeScript.TypeScriptClientGeneratorSettings settings, NJsonSchema.CodeGeneration.TypeScript.TypeScriptTypeResolver resolver) Line 49 C# NSwag.CodeGeneration.TypeScript.dll!NSwag.CodeGeneration.TypeScript.TypeScriptClientGenerator.GenerateFile(System.Collections.Generic.IEnumerable<NJsonSchema.CodeGeneration.CodeArtifact> clientTypes, System.Collections.Generic.IEnumerable<NJsonSchema.CodeGeneration.CodeArtifact> dtoTypes, NSwag.CodeGeneration.ClientGeneratorOutputType outputType) Line 96 C# NSwag.CodeGeneration.dll!NSwag.CodeGeneration.ClientGeneratorBase<NSwag.CodeGeneration.TypeScript.Models.TypeScriptOperationModel, NSwag.CodeGeneration.TypeScript.Models.TypeScriptParameterModel, NSwag.CodeGeneration.TypeScript.Models.TypeScriptResponseModel>.GenerateFile(NSwag.CodeGeneration.ClientGeneratorOutputType outputType) Line 89 C# NSwag.CodeGeneration.dll!NSwag.CodeGeneration.ClientGeneratorBase<NSwag.CodeGeneration.TypeScript.Models.TypeScriptOperationModel, NSwag.CodeGeneration.TypeScript.Models.TypeScriptParameterModel, NSwag.CodeGeneration.TypeScript.Models.TypeScriptResponseModel>.GenerateFile() Line 65 C# NSwag.Commands.dll!NSwag.Commands.CodeGeneration.SwaggerToTypeScriptClientCommand.RunAsync.AnonymousMethod__147_0() Line 390 C#

DDtMM commented 3 years ago

I had the same problem with version 13.7.0 and version 10.1.26 of NJsonSchema. I then tried downgrading and the problem persisted. My schema and the one provided by @HowCome were very different - he was using swagger 2.0, me OpenApi 3.1. That actually made it easy to find the one glaring thing we had in common. Under definitions/components.schemas we both had the following:

"JToken": {
   "type": "array",
   "items": {
       "$ref": "#/components/schemas/JToken"
   }
}

When I took this out the problem went away.

RicoSuter commented 3 years ago

If this persists, can someone create a PR with a unit tests where this is reproduced? With that it should be quite easy to fix here.

ivan-prodanov commented 3 years ago

Happened to me too. Generating schema for this causes stackoverflow exception unless depth is defined:

public class GenericClass<T>
{
    public List<T> GenericList { get; set; }
}
fabiorosadev commented 2 years ago

Hi guys!

Someone solved this problem? Happened to me here... Anyone have any trick?