Creating a json schema from a class containing EnumMap where all possible enum values are used as keys #376
-
I was playing a little with a library and got stuck when implementing a game configuration. My goal is to be able to start the game with a configuration saved as a file. To make sure the configuration is correct and easy to work with, I need to provide a schema for my config file. We can assume typical game stuff like resources, buildings, units, etc. which means I would like to operate on those abstractions in all the places in the source code, to avoid having concrete names as members or methods names. Thus I have a couple of enums declaring all possible concrete "names" of those abstractions such as wood, stone, food for resources, etc. Then those enums will be typically used as keys in maps to store associated values as per game configuration. Here's code extracted for the sake of this discussion: https://github.com/Uprzejmy/victools-jsonschema-reproducer/blob/master/src/main/java/victools/jsonschema/reproducer/App.java Take a look at the difference between serialized object and does the generated schema looks like {
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
"properties" : {
"trainingCostTable" : {
"type" : "object"
}
}
} Serialized object {
"trainingCostTable": {
"blacksmith": {
"trainingCosts": {
"wood": 100,
"food": 150,
"stone": 50
}
},
"farmer": {
"trainingCosts": {
"wood": 100,
"food": 150,
"stone": 50
}
}
}
} Now I know that the usage of EnumMap doesn't mean that all possible keys (enum values) should be used, but it would be nice to have such an option. One can create a json schema using my serialized object with proper "required" attributes just fine. Note that in my particular example having all enum values to be present in the json is not really necessary, as having a value set as 0 and not having it there at all is exactly the same thing. Would it be possible to add features for EnumMap, so that it is treated like a map with well-known keys (possibly making them mandatory in the schema)? Or to include the referenced enum in the schema as part of definitions and use it? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @Uprzejmy, I've created an example showcasing how your described scenario can be configured: {
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"$defs" : {
"JobDetails" : {
"type" : "object",
"properties" : {
"trainingCosts" : {
"properties" : {
"wood" : {
"type" : "integer"
},
"food" : {
"type" : "integer"
},
"stone" : {
"type" : "integer"
}
}
}
}
}
},
"properties" : {
"blacksmith" : {
"$ref" : "#/$defs/JobDetails"
},
"farmer" : {
"$ref" : "#/$defs/JobDetails"
}
}
} The custom definition provider could be further extended to cover any special requirements you may have, |
Beta Was this translation helpful? Give feedback.
Hi @Uprzejmy,
I've created an example showcasing how your described scenario can be configured:
https://github.com/victools/jsonschema-generator/blob/main/jsonschema-examples/src/main/java/com/github/victools/jsonschema/examples/EnumMapExample.java
The resulting schema looks like this: