joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc

Home Page:http://www.jsonschema2pojo.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Map generation from standard JsonSchema

NicolasLabrousse opened this issue · comments

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.

  • Is there any possibilities to achieve what we want in the current sate of the project ?

Thanks you !

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.

I'm going to close this as:

  • It's possible to do what you want using existingJavaType
  • Using existingJavaType should never break other JSON Schema tools
  • The output without existingJavaType already gives you classes that perform perfect data binding, and you're looking for a stylistic change in the Java (and we intentionally tend to avoid making changes or new configuration that relates purely a question of preferred Java style).