Jusas / NSwag.AzureFunctionsV2

NSwag for Azure Functions
MIT License
20 stars 5 forks source link

Unable to set property names to be generated in Camel Case. #10

Closed mcquiggd closed 4 years ago

mcquiggd commented 4 years ago

I am using Azure Functions v2; I am not using MVC. In my startup, I have the following code that correctly sets the behavior of NewtonSoft Json Serialization, to be ordered, and camel cased (the OrderedContractResolver inherits from CamelCasePropertyNamesContractResolver).

            JsonConvert.DefaultSettings = () =>
            {
                var settings = new JsonSerializerSettings();
                settings.Converters.Add(new StringEnumConverter());
                settings.ContractResolver = new OrderedContractResolver();
#if DEBUG
                settings.Formatting = Formatting.Indented;
#else
                settings.Formatting = Formatting.None;
#endif

                return settings;
            };
        }

I have a Function, to generate the Swagger Json:

        [OpenApiIgnore]
        [FunctionName("Swagger")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/json")]
            HttpRequest req, ILogger log)
        {
            var title = Assembly.GetExecutingAssembly().GetName().ToString().Replace(".", " ");
            var settings = new AzureFunctionsV2ToSwaggerGeneratorSettings { Title = title };

            var generator = new AzureFunctionsV2ToSwaggerGenerator(settings);

            var types = AzureFunctionsV2ToSwaggerGenerator.GetAzureFunctionClasses(Assembly.GetExecutingAssembly());
            var document =
                await generator.GenerateForAzureFunctionClassesAsync(types, null).ConfigureAwait(false);
            var json = document.ToJson();
            return new OkObjectResult(json);
        }

I have tried explicitly setting the "obsolete" property of

settings.GeneratorSettings.DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.CamelCase;

And also:

            settings.SerializerSettings = new JsonSerializerSettings();
            settings.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            var generator = new AzureFunctionsV2ToSwaggerGenerator(settings);

However, the generated json is not Camel Cased:

CreateProductRequest: {
type: "object",
properties: {
CompanyId: {
type: "string"
},
Description: {
type: "string"
},
HsCode: {
type: "string"
},
Name: {
type: "string"
},
SKU: {
type: "string"
}
}
},

Any ideas... ?

CharlieDigital commented 4 years ago

Same question; have not been able to get the output to be camelCased.

Were you able to resolve this?

RicoSuter commented 4 years ago

DefaultPropertyNameHandling Is deprecated. Use serializersettings property with a camel case contract resolver, see newtonsoft docs

mcquiggd commented 4 years ago

This is a while ago now, but I was actually using SerializerSettings:

settings.SerializerSettings = new JsonSerializerSettings();
settings.SerializerSettings.ContractResolver = new   
  CamelCasePropertyNamesContractResolver();

var generator = new AzureFunctionsV2ToSwaggerGenerator(settings);

I believe this was not working, because I was using a common Startup with a FunctionHost. For example, when using MVC, you can set that behaviour as a global setting in one of extension methods (such as AddControllers), and that is then propagated.

The sample code from NewtonSoft does work in a quick Function App I just tested:

           // settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
            JsonConvert.DefaultSettings = () =>
                new JsonSerializerSettings
            { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() };

            var s = new Staff
            {
                FirstName = "Eric",
                LastName = "Example",
                BirthDate = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
                Department = "IT",
                JobTitle = "Web Dude"
            };

            var json = JsonConvert.SerializeObject(s);

            Console.WriteLine(json);

I'll need to find my original code to verify if this works in a Startup class (again, without using MVC), but for now I will close this... thanks.