Skip to content

add concrete schema for discriminated types into 'oneOf' for base type #3293

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

EvgeniyZ
Copy link
Contributor

@EvgeniyZ EvgeniyZ commented Mar 5, 2025

fix for #3166

Details on the issue fix or feature implementation

I am not happy how it looks like in Swagger UI, I open for any suggestions

here's how looks Animal class with subtypes. Having refs to himself can easily lead to such unpleasant view
image

BaseType with SubSubType link
image

@martincostello
Copy link
Collaborator

The tests seem quite broken - it looks like they've got into a cycle somewhere.

add concrete schema for discriminated types into 'oneOf' for base type
@EvgeniyZ EvgeniyZ force-pushed the specify-oneOf-discriminated-polymorph branch from b6ea2f0 to 2a6af62 Compare March 10, 2025 19:44
@EvgeniyZ
Copy link
Contributor Author

EvgeniyZ commented Mar 10, 2025

StackOverflow exception is caused somewhere inside NJsonSchema.JsonSchema, here's truncated exception

NSwag command line tool for .NET Core Net80, toolchain v14.2.0.0 (NJsonSchema v11.1.0.0 (Newtonsoft.Json v13.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: C:\Users\Evgenii\.nuget\packages\nswag.msbuild\14.2.0\tools\Net80
Swagger JSON/YAML successfully written to E:\development\swashbuckle.aspnetcore\test\WebSites\NswagClientExample\swagger_net9.0.json
dotnet "C:\Users\Evgenii\.nuget\packages\nswag.msbuild\14.2.0\buildTransitive\../tools/Net90/dotnet-nswag.dll" openapi2csclient /input:swagger_net9.0.json /namespace:NSwagClient /output:NSwagClient/Client.cs
NSwag command line tool for .NET Core Net90, toolchain v14.2.0.0 (NJsonSchema v11.1.0.0 (Newtonsoft.Json v13.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: C:\Users\Evgenii\.nuget\packages\nswag.msbuild\14.2.0\tools\Net90
Stack overflow.
   at System.Collections.Generic.List`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].set_Capacity(Int32)
   at System.Collections.Generic.List`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].AddWithResize(System.__Canon)
   at NJsonSchema.JsonSchema.GetActualSchemaReferences(System.Collections.Generic.List`1<NJsonSchema.JsonSchema>)
   at NJsonSchema.JsonSchema.GetActualSchema(System.Collections.Generic.List`1<NJsonSchema.JsonSchema>)
   at NJsonSchema.JsonSchema.get_ActualSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.ObjectModel.ObservableCollection`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>)
   at NJsonSchema.JsonSchema.get_ActualTypeSchema()
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema.IsNullable(NJsonSchema.SchemaType)
   at NJsonSchema.JsonSchema+<>c.<get_ActualTypeSchema>b__255_2(NJsonSchema.JsonSchema)
   at NJsonSchema.ObservableCollectionExtensions.FirstOrDefault[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, ...
   ...

I assume it crashes while trying to generate C# client via NSwag, in the NSwagClientExample.csproj

  <Target Name="NSwag" AfterTargets="SwaggerToFile">
    <Exec Command="$(_NSwagTool) openapi2csclient /input:swagger_$(TargetFramework).json /namespace:NSwagClient /output:NSwagClient/Client.cs" />
  </Target>

however, I am not able to trace where to find source code for NJsonSchema.JsonSchema.GetActualSchemaReferences method call (or any of the stacktrace above)

@martincostello I appreciate any tips if you have any

@martincostello
Copy link
Collaborator

Looks like its gotten in a self-referential loop (e.g. type A has a property of type A in it) and causing a stack overflow in NSwag. Maybe look at the code there?

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

Successfully merging this pull request may close these issues.

2 participants