azure-functions-openapi-extension icon indicating copy to clipboard operation
azure-functions-openapi-extension copied to clipboard

Model casing is changed

Open gsimardnet opened this issue 4 years ago • 6 comments

I defined an XML request like the following : [OpenApiRequestBody(contentType: "text/xml", bodyType: typeof(Model.MyClass))]

public class MyClass
{
    public MySubClass MySubClass { get; set; }
}

public class MySubClass
{
    public string MyProperty { get; set; }
}

Then when I look in the generated UI, the casing I have defined has changed: <?xml version="1.0" encoding="UTF-8"?> <myClass> <mySubClass> <myProperty>string</myProperty> </mySubClass> </myClass>

So far, the only solution I have found is to redefine the name of each property using the JsonProperty decorator. For instance: [JsonProperty("MyProperty")]

Also does it make sense to use that decorator when my request is 'text/xml' ? It looks like this extension is made for JSON and XML is an afterthought.

gsimardnet avatar Apr 06 '21 14:04 gsimardnet

At the moment it doesn't look like there is a way to override this using the main package that provides the templates, but if you copy in the three OpenAPI template files (OpenApiConfiguration, OpenApiHttpTrigger, and OpenApiHttpTriggerContext) and reference the Microsoft.Azure.WebJobs.Extensions.OpenApi.Core package instead, you can then change NamingStrategy on OpenApiHttpTriggerContext to be new DefaultNamingStrategy() instead of using CamelCaseNamingStrategy

ssa3512 avatar Apr 06 '21 16:04 ssa3512

@gsimardnet Thanks for the issue! You can use the suggestion by @ssa3512 as a workaround. In the meantime, I'll have a look at the configuration that can be overrideable, which is on:

https://github.com/Azure/azure-functions-openapi-extension/blob/7f70286964d0abc3fc88be32da11f50cce6652ba/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/IOpenApiConfigurationOptions.cs#L10-L21

justinyoo avatar Apr 06 '21 23:04 justinyoo

@justinyoo if you want to assign this to me I should be able to get a PR up later this week or weekend.

ssa3512 avatar Apr 13 '21 00:04 ssa3512

@ssa3512 Thanks for your support!

justinyoo avatar Apr 13 '21 09:04 justinyoo

This is still an issue, or atleast also an issue in .NET isolated using 'Microsoft.Azure.Functions.Worker.Extensions.OpenApi'.

'UseNewtonsoftJson' uses the 'CamelCasePropertyNamesContractResolver', see:

https://github.com/Azure/azure-functions-openapi-extension/blob/ab184cbf3c8ff16378cfa00fa1cb23cb58ac1727/src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Extensions/FunctionsWorkerApplicationBuilderExtensions.cs#L37

This seems like a strange choice as default, as it differs from default serializer (System.Text.Json) in a new isolated Function App.

For anyone looking to disable this, you can do the following:

worker.UseNewtonsoftJson(new JsonSerializerSettings 
{
    ContractResolver = new DefaultContractResolver()
});

Is there a reason camel case is defaulted?

Ausxor avatar Dec 22 '23 00:12 Ausxor

Implemented and opened as PR #646. I agree that camel case as default is possibly not the optimal choice (personally not my desired one anyway), but changing that would obviously be a breaking change.

CSymes avatar Feb 19 '24 05:02 CSymes