Hi guys! Thank you for the great work.
I have a problem with json model in Model Schema when define my example or default in schema.
SchemaFilter
public class ModelVmSchema : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
schema.@default = new ModelVm();
}
}
Global.asax
public static class FormattersConfig
{
public static void Config(MediaTypeFormatterCollection collection)
{
collection.Add(new FileMediaFormatter());
collection.JsonFormatter.SerializerSettings.ContractResolver = new CamelCase();
collection.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
JsonConvert.DefaultSettings = () =>
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
settings.ContractResolver = new CamelCase();
return settings;
};
}
}
Class CamelCase inherits from CamelCasePropertyNamesContractResolver.
If I don't define my custom example or default it works fine. I can see Model Schema with camelCase format. But when I define it in ModelVmSchema I see Model Schema on UI without any changes.
Is it bug or do I have to do something else for it?
I need to define custom example or default value because I have default values for properties in models.
Closing as this is fundamentally a dup of #694. In lieu of the internal fix you can workaround by defining your examples as anonymous objects with the desired casing.
@domaindrivendev
How would you go about the work around for using camel case with a Schema Filter? Can I change the property name in schema.properties? I am running into the same issue and am having trouble finding a solution.
Maybe it will be useful for somebody. I use the following code for solving this problem:
public static class ObjectAssistant
{
public static object ToCamelCaseObject(this object obj)
{
var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore });
return JsonConvert.DeserializeObject(json);
}
}
And using:
public class ModelRequestVmSchema : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
schema.example = new ModelRequestVm().ToCamelCaseObject();
}
}
@kongres
Thanks! This is exactly what I was looking for
Most helpful comment
Maybe it will be useful for somebody. I use the following code for solving this problem:
And using: