Skip to content
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

NSwag code generates the wrong type for enum attributes #2107

Closed
possum64 opened this Issue Apr 14, 2019 · 7 comments

Comments

Projects
None yet
2 participants
@possum64
Copy link

possum64 commented Apr 14, 2019

I have a DTO called 'Order' which contains an attribute called 'DistributionCenterRollup' that is defined to be of type DistributionCenterRollupType' via the schema reference below:

"$ref": "#/components/schemas/ChannelAdvisor.RestApi.V1.Models.DistributionCenterRollupType"

However, this attribute is incorrectly generated with the completely wrong type as shown below:

public DistributionCenterRollup DistributionCenterRollup { get; set; }

That is, the code generator appears to duplicate the attribute name for the enum type instead of using the referenced enum type. Obviously this will not work - but it does insidiously compile and run because a DistributionCenterRollup class containing the to/from JSON convertors is also generated - a (de)serialisation error inevitably occurs during code execution.

This is a show-stopper for me as I have a large number of referenced enum types in my JSON.

Hopefully this is a simple fix - even if someone could even point me to the relevant part of the code please, as if I could get this fixed the tool would be invaluable (instead of currently worthless :-( )

@possum64 possum64 changed the title NSwag sets the wrong type for enum attributes NSwag code generates the wrong type for enum attributes Apr 14, 2019

@RicoSuter

This comment has been minimized.

Copy link
Owner

RicoSuter commented Apr 14, 2019

Can you post a sample spec? (This all is handled in njsonschema)

@RicoSuter

This comment has been minimized.

Copy link
Owner

RicoSuter commented Apr 14, 2019

But it looks correct and should use the referenced schema and schema name... looks like a special case or a bug

@possum64

This comment has been minimized.

Copy link
Author

possum64 commented Apr 14, 2019

Its a bug.

I'd say there is an incorrect call somewhere that wrongly passes the attribute name instead of the attribute type name when the attribute is an enum.

Here's another plain example from my Order DTO. The type defined for OrderTaxType, ShippingTaxType, and GiftOptionsTaxType is the same schema reference to the enum type 'TaxType' - but the generator ignores those type references (altho it does generate the TaxType enum class itself). Instead, it generates JSON convertor types for each attribute using the attribute name (which seems pointless), and then declares each attribute type as being the JSON convertor type instead of TaxType! (obviously that is not going to work):

image

image

image

Please also note that the generated code in the JSON convertor type also wrongly converts itself instead of TaxType! AFAIKS there should be only 1 convertor class per enum - not a converter class per enum attribute.

@RicoSuter

This comment has been minimized.

Copy link
Owner

RicoSuter commented Apr 15, 2019

The problem is probably the anyOf, try to remove it and use $ref directly or use oneOf... just to see if that solves the problem... i will then check how to handle this correctly...

@possum64

This comment has been minimized.

Copy link
Author

possum64 commented Apr 16, 2019

Replacing anyOf with oneOf fixes the NSwag generation issue. Thanks.

However, this OpenAPI file was itself generated by Microsoft's OData to OpenAPI convertor, so unfortunately these 2 tools are incompatible at the moment...

@RicoSuter

This comment has been minimized.

Copy link
Owner

RicoSuter commented Apr 16, 2019

Fixed with the above commit, will be in the next version soon.

@possum64

This comment has been minimized.

Copy link
Author

possum64 commented Apr 16, 2019

Excellent! Thanks Rico.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.