0

Has anyone had much experience converting Swagger API definitions to C#.NET client code?

Mostly, Swagger Editor works ok, but the issue we have is that the generated code for some calls has a bunch of required-but-nullable arguments. Eg (truncated argument list for illustration only):

GetCustomers(bool? includeDeceased, string postalCode, string id, string dateAdded)

We would like the generated parameter list to be defined with default values to avoid having to explicitly provide null arguments. So something like this:

GetCustomers(bool? includeDeceased = null, string postalCode = null, int? id = null, string dateAdded = null)

This allows us to make the following call:

GetCustomers(postalCode: "12345")

Rather than:

GetCustomers(includeDeceased: null, postalCode="12345", id: null, dateAdded: null)

We've tried adding "default": null to the Swagger call definitions where appropriate but this doesn't result in the desired code (in fact, no change, so probably not supported).

Short of manually modifying the resulting client code – which we want to avoid so we can quickly and easily update our code to the latest API definitions – we're a bit stuck.

2
  • I'm assuming you own/develop the API and can change it. If so, have a look at this SO item on how you can create your yaml to say your parameter is nullable, with a default value (see comment to the answer). Nullable Fields in Swagger Commented Nov 15, 2017 at 2:36
  • @FrankFajardo Nope, I don't. But I could request a correction to the Swagger definition or probably script changes to it if necessary. But the issue is actually the Swagger Editor CsharpDotNet2 client code generator as far as I can tell, as the csharp one works as expected. Refer to my answer below. Commented Nov 15, 2017 at 2:54

1 Answer 1

1

Well, it seems that the Swagger Editor CsharpDotNet2 client code generator is the culprit here.

I just tried the bog-standard csharp client code generator and it added = null to all arguments not marked "required": true.

So I guess it's manually edit the CsharpDotNet2 generator or use the csharp one if you can.

UPDATE And, in case you're wondering, it turns out that the csharp generator is actually the newer one, targeted at .NET 4.5. I was thinking that if one had "DotNet" in the name and the other didn't that that was the one to go for. Silly me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.