RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.7k stars 1.24k forks source link

Question about oneOf #3260

Open SebastianStehle opened 3 years ago

SebastianStehle commented 3 years ago

Hi,

I have the following schema: https://app.notifo.io/api/docs/index.html?url=/api/openapi.json

It works very great with NSwag and typescript/C# code generation, but many other code generators seem to have issues with oneOf+$ref and generate classes or empty interfaces for all nested objects.

Is this a problem of the code generators or something that can be improved?

jeremyVignelles commented 3 years ago

What's your question if it works well with NSwag? oneOf is not something that is very easy to work with in a strong-typed language like C#.

SebastianStehle commented 3 years ago

I need a ruby and java client and these code generators seem to have issues.

e.g. java code is created like this:

@SerializedName("preformatted")
private OneOfPublishDtoPreformatted preformatted = null;

public interface OneOfPublishDtoPreformatted {
}

and C# works fine with:

public NotificationFormattingDto Preformatted { get; set; }

public partial class NotificationFormattingDto 
{
   ...    
   public LocalizedText Subject { get; set; }
}

So the question is :

I have tried these code generators: https://editor.swagger.io/

jeremyVignelles commented 3 years ago

It might be because NSwag always generates a oneOf level, which is not expected by the code generators. Not sure what can be done on the generator side.

However, on NSwag, you can use the OpenApi document callback to edit the generated document to make it look the way you want.

SebastianStehle commented 3 years ago

I found this: https://github.com/RicoSuter/NJsonSchema/search?q=AllowReferencesWithProperties

RicoSuter commented 3 years ago

Yeah, we use oneof if there are more props alongside $ref (because json pointer specifies that $ref must be alone).. however many tools cannot handle this (also azure api mgmt) and you can turn it off with this cfg

SebastianStehle commented 3 years ago

Yes, I have forgotten to mention that it works as expected, but why is this not the default?

RicoSuter commented 3 years ago

but why is this not the default?

Because it technically produces invalid JSON Schemas...

But I asked myself the same question. Changing this default would be a breaking change. But maybe we should do that in the next minor NJS/NSwag release (10.4 / 13.10).