OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.83k stars 6.58k forks source link

[REQ] [csharp],[csharp-netcore] Add Parameter Less Default Constructors For Model Classes #8162

Open GauravQ opened 3 years ago

GauravQ commented 3 years ago

I've been using openapi-generator-cli for generating APIClient Nugets to be used in .Net Framework, .Net Core Web Apps.

But I'm not able to use generated models as parameter in a [Post] Action methods as default conversion from Post Body (JSON) to Class Object requires a parameter less constructor (a default constructor). But the generated models only have a parameterized constructor, which then results in an error.

Probable solution

Adding a default constructor in all Model classes will be very helpful.

As of now the only solution is to create similar Model classes in Web App itself and when calling an API convert that Web App's Model To APIClient's Model (generated model).

Here's how its being generated using v4.3.1

image

And here's how it could be generated to resolve the issue. having a default constructor with another parameterized constructor

image

Not sure how much work is needed here, but i would really appreciate if we can have this added.

auto-labeler[bot] commented 3 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

pkunze commented 3 years ago

@GauravQ we are facing the same problem. Did you find an (automated) workaround for this? Maybe some mustache template?

wing328 commented 3 years ago

@GauravQ can you please submit a PR with the suggested change and we'll review accordingly?

GauravQ commented 3 years ago

@wing328 Sure, I'll try to spend some time on it. Maybe this weekend.

prajon84 commented 2 years ago

@GauravQ @pkunze : Did you find any workaround for this?

jfeltesse-mdsol commented 2 years ago

For info, we have a fix for that for the csharp-netcore variant, we're going to submit a PR in the coming weeks.

devhl-labs commented 2 years ago

[Post] Action methods as default conversion from Post Body (JSON) to Class Object requires a parameter less constructor (a default constructor).

The issue here is not clear to me. It sounds like there is JSON that is not deserializing.

alec-petersen commented 1 year ago

@devhl-labs The functionality like TypeAdapter.Adapt that allows you to easily cast one identical model object to another identical model object requires a default constructor, which is currently only created if you specify one or more of the model properties as required (which seems like strange behavior but I don't understand all the in and outs). Was there any progress on adding a default constructor or providing an option to?

EDIT the problem actually turned out to be (in my case) how the generator generated code differently for property named 'SOME_THING' and 'some_thing'. So would not be fixed by a parameterless constructor.