jsonschema2pojo icon indicating copy to clipboard operation
jsonschema2pojo copied to clipboard

Usage of `maxProperties` not possible

Open bsoaressimoes opened this issue 10 months ago • 4 comments

Hello,

I tried to generate pojo using the maxProperties field on an object of type object, but it seems not handled at all in jsonschema2pojo plugin. As a workaround we could use maxItem instead.

The problem with this solution is the fact that maxItem should be applied on Array while maxProperties is dedicated to objects. So the json we declare is wrong.

Do you plan to add the maxProperties behavior in the plugin?

In case you need my schema is:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "Some Id",
  "type": "object",
  "properties":
  {
    "product":
    {
      "description": "Some description",
      "type": "string"
    },
    "attr":
    {
      "description": "Some attribute",
      "type": "object",
      "existingJavaType": "java.util.Map<String,String>",
      "patternProperties":
      {
        "^.*$":
        {
          "type": "string"
        }
      },
      "maxProperties":3,
    }
  },
  "required": [ "product" ]
}

bsoaressimoes avatar Feb 05 '25 10:02 bsoaressimoes

Hi

I tried to generate pojo using the maxProperties field on an object of type object, but it seems not handled at all in jsonschema2pojo plugin.

maxProperties is indeed not supported

As a workaround we could use maxItem instead. The problem with this solution is the fact that maxItem should be applied on Array while maxProperties is dedicated to objects.

Statements above are contradicting each other. Declaring maxItems can't be used as a workaround since as noted in spec:

The maxItems keyword is used to specify the maximum number of items allowed in an array. It can be used to define constraints on the size of an array within an array instance.

Do you plan to add the maxProperties behavior in the plugin?

Not sure what maxProperties would translate into as there's a handful of built-in bean validation constraints.

unkish avatar Feb 11 '25 17:02 unkish

I see that you have overridden this value to be java.util.Map<String,String>, so the @Size annotation from the bean validation spec should work here. It seems like if the value type is java.util.Map and maxItems is present, we can add @Size.

joelittlejohn avatar Feb 11 '25 18:02 joelittlejohn

I'm guessing that maxItems is ignored by most schema validators if the property is not an array, so it acts as a workaround to make the @Size annotation appear.

joelittlejohn avatar Feb 11 '25 18:02 joelittlejohn

The maxItems works well to apply the constraint we need in our use case. But since the field is an object, the maxProperties should be the one to use regarding json rules. I just wanted to confirm if the pluggin would integrate this behavior.

bsoaressimoes avatar Feb 13 '25 16:02 bsoaressimoes