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

How to create a simple pojo class with map attribute, not able to find any example. #1582

Open sjain700 opened 10 months ago

sjain700 commented 10 months ago

I want to create a simple pojo class like:

public class VendorFields {
    private String id;
    private String vendorName;
    private Map<String, CustomClass> vendorAttributes = new HashMap<String, vendorAttributes>(); //Not able to create this.
}

public class CustomClass{
String URL;
String endPoint;
}

the Json i am using:

{
  "title": "Resource",
  "description": "Resource Object",
  "type": "object",
  "properties": {
    "id": {
      "description": "id",
      "type": "string"
    },
    "vendorName": {
      "description": "Vendor Name",
      "type": "string"
    },
    "vendorAttributes": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/definitions/CustomClass"
      }
    }
  },
  "definitions": {
    "CustomClass": {
      "properties": {
        "URL": {
          "description": "URL resource",
          "type": "string"
        },
        "endPoint": {
          "description": "URL resource",
          "type": "string"
        }
      }
    }
  },
  "required": [
    "vendorName"
  ]
}

but getting 3 classes as output:

public class VendorFields {
    private String id;
    private String vendorName;
    private VendorAttributes vendorAttributes;  // this is not required;
    private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>(); //this is also wrong.
}
public class VendorAttributes {  
    private Map<String, CustomClass> additionalProperties = new LinkedHashMap<String, CustomClass>();
}

public class CustomClass {
    private String url;
    private String endPoint;
}

What property to set or this is the expected behavior?

joelittlejohn commented 10 months ago

Hi. When posting code snippets like this, please use backtick blocks like this:

https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#syntax-highlighting

It makes things a lot easier to read. I have added them on your example.

Could you try adding this inside the vendorAttributes schema:

"existingJavaType":"java.util.Map<String, CustomClass>",

You will probably have to pull CustomClass out into its own file so that this gets generated too.

rkand4 commented 9 months ago

I wanted just a map of List values like below: I just wanted a field to be private Map<String, List<String>> varieties

          "varieties" : {
            "type" : "object",
            "existingJavaType" : "java.util.Map<String,List<String>>",
            "additionalProperties" : {
              "type" : "array",
              "items" : {
                "type" : "string"
              }
            }
          }

But I see that the generated code fails compilation because it doesn't recognize java.util.List and doesn't import in the generated code :( Any help please @joelittlejohn ? Thanks !

unkish commented 9 months ago

Hi

Please use fully qualified names when referring to types that are outside same or java.lang package, eg.:

    "varieties": {
      "type": "object",
      "existingJavaType": "java.util.Map<String, java.util.List<String>>",
      "additionalProperties": {
        "type": "array",
        "items": {
          "type": "string"
        }
      }
    }