asyncapi / saunter

Saunter is a code-first AsyncAPI documentation generator for dotnet.
https://www.asyncapi.com/
MIT License
199 stars 56 forks source link

Add support for case-sensitive object serialization #106

Closed nyrk closed 3 years ago

nyrk commented 3 years ago

Currently, it seems that serialization produces schemas in which the first letter of the properties is lowercase, even if in the original class is upper case.

For instance, if I have a class

public class MyWonderfulClass
{
        [Required]
        public string Name { get; protected set; }

        [Required]
        public string Colour { get; protected set; }
}

Saunter produces something like the following


"myWonderfulClass": {
        "id": "myWonderfulClass",
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name",
          "colour"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1
          },
          "colour": {
            "type": "string",
            "minLength": 1
          }
        }

(in which the first letter of each property has been converted to lower-case).

It would be useful to have a setting to keep the original class case convention (i.e. to keep property case unchaged).

For instance, using the following code in a command-line console app:


JsonSchemaGeneratorSettings jsetting = new JsonSchemaGeneratorSettings();
jsetting.FlattenInheritanceHierarchy = true;
JsonSchema schema = JsonSchema.FromType<MyWonderfulClass>(jsetting );
string schemaString = schema.ToJson();
Console.WriteLine(schemaString);

it possible to obtain the schema serialization in which the first letter of each property is uppercase (reflecting the original class data-model).

JanEggers commented 3 years ago

https://github.com/tehmantra/saunter/blob/22fe8913ffe380af3d2f93004a8ce556b7fdf1e4/src/Saunter/AsyncApiOptions.cs#L52-L62

you need to set the asyncapioptions.JsonSchemaGeneratorSettings.SerializerSettings.ContractResolver

m-wild commented 3 years ago

I was trying to get this working, looks like you need to replace the whole SerializerSettings object, you can't just set the ContractResolver 🤷‍♂️

services.AddAsyncApiSchemaGeneration(options =>
{
    options.JsonSchemaGeneratorSettings.TypeNameGenerator = new DefaultTypeNameGenerator();
    options.JsonSchemaGeneratorSettings.SerializerSettings = new JsonSerializerSettings
    {
        ContractResolver = new DefaultContractResolver(),
    };
}

I will add some examples to the README so no one ever has to waste an hour to discover this too 😅

nyrk commented 3 years ago

Hi guys, thanks for your prompt reply. @tehmantra : your hint fits perfectly my needs: great! 🥇

options.JsonSchemaGeneratorSettings.TypeNameGenerator = new DefaultTypeNameGenerator();
options.JsonSchemaGeneratorSettings.SerializerSettings = new JsonSerializerSettings
{
   ContractResolver = new DefaultContractResolver(),
};
options.JsonSchemaGeneratorSettings.SerializerSettings.Formatting = Formatting.Indented;

I think it could be helpful to add this piece of information in the README.

Thank you very much for your help.