Skip to content

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

Closed
EvgeniyZ wants to merge 1 commit intodomaindrivendev:masterfrom
EvgeniyZ:specify-oneOf-discriminated-polymorph
Closed

add concrete schema for discriminated types into 'oneOf' for base type#3293
EvgeniyZ wants to merge 1 commit intodomaindrivendev:masterfrom
EvgeniyZ:specify-oneOf-discriminated-polymorph

Conversation

@EvgeniyZ
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

@github-actions
Copy link
Copy Markdown
Contributor

This pull request is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further changes are made.

@github-actions github-actions Bot added the stale Stale issues or pull requests label May 10, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This pull request was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions Bot closed this May 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale issues or pull requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants