Allow to customize the Newtonsoft.Json.Converters.StringEnumConverter
Closed this issue · 0 comments
Hello everyone.
This is a question or a feature requests, it depend on how it is possible to solve the problem that i will expose now.
When a swagger.json file expose a definition like the following:
"ChatType": { "enum": [ "SINGLE", "MULTIPLE", "UNKNOWN" ], "type": "string" },
Unchase.OpenAPI generate the following code (near each of the properties of type "ChatType"):
[Newtonsoft.Json.JsonProperty("Type", Required = Newtonsoft.Json.Required.Always)] [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)] [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] public ChatType Type { get; set; }
Being specified as annotation, the converter StringEnumConverter is ALWAYS used, even if i specify a custom converter in the configuration parameter "JsonConverters".
This works as long as the "ChatType" does not change its values, aka adding a new one; if i add a new value (like "EXTERNAL") and I DO NOT RE-GENERATE the client, then the original client will use the StringEnumConverter to try to translate the new string into an enum value, failing because StringEnumConverter throw an exception when it does not find a corresponding value.
I wrote my own converter (inheriting StringEnumConverter and using "UNKNOWN" in case of new values), but the only way in which i can use it is to manually replace every occurrencies of StringEnumConverter in the client generated code with mine AFTER each regeneration... which is risky and a bit outside the purpose of an automatic code generator.
If there is no alternative (i don't know if there are), can you please add a configuration option that allow to replace the default StringEnumConverter with a custom one during the generation of the client code?
Thanks in advance.