Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sjain700 opened this issue Jan 16, 2024 · 3 comments

Comments

@sjain700
Copy link

sjain700 commented Jan 16, 2024

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
Copy link
Owner

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
Copy link

rkand4 commented Feb 13, 2024

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
Copy link
Collaborator

unkish commented Feb 14, 2024

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"
        }
      }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants