feature: support convert class name to snake_case json key
for example:
class AbcTech {...}
convert to
{"abc_tech": }
Hi @shijinkui,
It should be straightforward to introduce something like that for definition keys. Will just need to make sure it works for various naming strategies then.
Some items need to be considered:
- If this is configurable it should still be enforced to be URI compatible (as demanded by the JSON Schema Specification or even stricter when adhering to the OpenAPI Specification). Currently that is achieved by replacing certain characters (like
</>/,) and removing others. For the Snake Case in particular that shouldn’t be an issue. - When nested classes are encountered, the declaring class‘ name is added as prefix with a
$symbol as separator. E.g.Map$Entry. Is there some special handling needed here? Should it be possible to not add this prefix? - What about duplicate names then? When encountering multiple classes with the same name in different packages or in scenarios where different custom definitions are applied for the same type, the definition keys currently get an automatic counter separated by a dash, e.g.
MyClass-1/MyClass-2or here thenmy_class-1/my_class-2. Would it be necessary for this suffix to be configurable as well? And if yes, what about cases where that leads to name clashes again?
The above are reasons for this not having been implemented yet. I‘m open for any ideas/suggestions on the above.
I also would like to have extended support in this area. In my case it is about changing the name because the name of the Java class is not good enough to be used in the schema. The classes I have are named *Bean, which is the implementation of an interface.
For example
class StepBean implements Step {
should be lead to the schema
"definitions" : {
"Step" : {
"type" : "object",
"properties" : {
....
I can do some work in this area. In my project I already made an implementation for my need. We can see whether this is generic enough.
HI @shijinkui,
I've created an implementation proposal for this in GH-113, introducing a new configuration option for a SchemaDefinitionNamingStrategy, which gives you (almost) full control over what names/keys are used in the definitions/$defs. For compatibility's sake any illegal characters are replaced/removed afterwards.
For the Snake Case in particular you could use the Jackson PropertyNamingStrategy to avoid having to define complicated rules from scratch:
PropertyNamingStrategyBase jacksonSnakeCase = new PropertyNamingStrategy.SnakeCaseStrategy();
SchemaDefinitionNamingStrategy snakeCaseStrategy = new DefaultSchemaDefinitionNamingStrategy() {
@Override
public String getDefinitionNameForKey(DefinitionKey key, SchemaGenerationContext generationContext) {
return jacksonSnakeCase.translate(super.getDefinitionNameForKey(key, generationContext))
.replaceAll("<_", "<")
.replaceAll(", _", ",");
}
};
configBuilder.forTypesInGeneral()
.withDefinitionNamingStrategy(snakeCaseStrategy);
The two .replaceAll() there are to improve the output for generic types a bit, but you may want to adjust those differently. E.g.
- A "normal" type like
BigDecimalwill be represented as"big_decimal". - A parameterised type like
Map<String, BigDecimal>will be represented as"map(string,big_decimal)"or (if you have theOption.PLAIN_DEFINITION_KEYSenabled, which disallows parentheses and commas)"map_string.big_decimal_"
As it's hard for me to judge how those parameterised types should be represented in snake case (or any other common alternative), I'm not planning to include pre-defined strategies for those. However, using the PropertyNamingStrategy implementations provided by Jackson already give you a good starting point.
@CarstenWickner thanks