Fabrizio P
Fabrizio P

Reputation: 251

declare java.util.Map into json-schema

I need to map a java.util.Map instance into a JSON-schema that is used by org.jsonschema2pojo maven plugin to create a POJO.

I didn't find a good and simple solution for this.

Could someone help me please?

This is my actual json-schema file

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Response",
    "description": "A Response object",
    "type": "object",
    "properties": {
        "result": {
            "type": "string",
            "description": "describes response status"
        },
        "msg": {
            "type": "string",
            "description": "user msgs"
        }
    },
    "required": ["result"],
    "additionalProperties": false
}

I need to add a field "errors" that is converted into a java.util.Map<String, String> in Java.

Upvotes: 1

Views: 2118

Answers (2)

JStefan
JStefan

Reputation: 193

It's an oldi but if you're looking for a Map property and not going for the additionalProperties option, it is facilitated:

{
    "type" : "object",
    "properties" : {
        "myProperty" : {
            "existingJavaType" : "java.util.Map<String,Integer>",
            "type" : "object"
        }
    }
}

Which will give you:

@JsonProperty("myProperty")
private Map<String, Integer> myProperty;

See existingJavaType

Upvotes: 0

hagbard
hagbard

Reputation: 695

AFAIK additionalProperties does the job. You can declare an errors property of type Map<String, Object> for example like this (this is yaml now):

...
properties:
  errors:
    type: object
    additionalProperties:
      type: object

You don't specify the type of the keys, since this describes a json document, which naturally has strings as keys on objects.

instead of type: object you can also do type: string for Map<String, String> or reference another definition if you have your own type as values in that map.

Upvotes: 0

Related Questions