joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Map generation from standard JsonSchema #1549

Closed NicolasLabrousse closed 1 year ago

NicolasLabrousse commented 1 year ago

Hello,

In our project we have a jsonSchema of the following form :

{
  "$schema": "http://json-schema.org/draft-06/schema#", 
  "$ref": "#/definitions/component-schema-config",
  "definitions": {
    "component-schema-config": {
      "type": "object",
      "additionalProperties": false,
      "description": "Mandatory. Component configuration",
      "properties": {
        "concurrency-policies": {
           "type": "object",
                "description": "Map with Procedure name as key and list of procedure concurrency policy as value",
                "additionalProperties": {
                  "type": "array",
                  "items": {
                     "type": "string",
                  }
                }
        },
        "title": "concurrency-policies"
      },
    }
  }
}

We are using the generated class from this schema to map a configuration file automatically with Springboot. To be able to do that, the generated class member must have the exact same name as the one in configuration file. Our problem is with map, in this example, "concurrency-policies". When executing the generation, we end up with something like this :

public class conf
{
private ConcurencyPolicies concurencyPolicies;
}

public class ConcurencyPolicies
{
private Map<String,String> additionalProperties;
} 

But we want something like this :

public class conf
{
private Map<String, String> concurencyPolicies;
}

In order to do that, we can modify our JsonSchema with the property "existingJavaType". This modification will break the compatibitly with other jsonSchema tools and that's not a possibility for us as this jsonSchema is part of the documentation of our service.

Thanks you !

joelittlejohn commented 1 year ago

Hi Nicolas. Adding existingJavaType should not break compatibility with other tools. JSON schemas allows extension properties, so no tool should fail if it sees a property on a schema that it does not recognise.

joelittlejohn commented 1 year ago

I'm going to close this as: