RicoSuter / NSwag

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

code generation for TypeScript about JsonExtensionData #1541

Open elementuse opened 5 years ago

elementuse commented 5 years ago

Is JsonExtensionData code generation not supported in typescript?

RicoSuter commented 5 years ago

You're talking about this property in C#?

https://github.com/RSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid#L77

elementuse commented 5 years ago

in typescript. is this code generator support?

RicoSuter commented 5 years ago

Yes, but you need something like this in typescript?

elementuse commented 5 years ago

yes. using typescript, my data was filtered out by the proxy class.

RicoSuter commented 5 years ago

Yep, not implemented yet...

elementuse commented 5 years ago

How to avoid this problem and let my data display? is there any good way?

RicoSuter commented 5 years ago

I think the only way to fix that right now is to use interfaces instead of classes for DTOs...

Sharparam commented 3 years ago

What's the status on this?

Not sure if it belongs here or in a new issue, but the code in the generated TypeScript code to handle JsonExtensionData doesn't seem to follow spec. It expects there to be a property "extensions" in the JSON data but that will not be there in correctly generated JSON, as specified in the documentation for the property Extensions on ProblemDetails:

Extension members appear in the same namespace as other members of a problem type.

So for example, given this constructed ProblemDetails:

new ProblemDetails
{
    // ...
    Extensions =
    {
        ["CustomExtension"] = "custom value"
    }
};

The following JSON will be generated (standard fields omitted):

{
    "CustomExtension": "custom value"
}

However, NSwag's TypeScript code expects this:

{
    "extensions": {
        "CustomExtension": "custom value"
    }
}

Not sure how the C# JSON libraries handle them, but I suppose the way to handle it is to treat any root-level field on the JSON that doesn't have a matching property in the TypeScript class/interface as an extension and stuff it into the "extensions" property on the TypeScript class.