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

Usage of maxProperties not possible #1665

Open
bsoaressimoes opened this issue Feb 5, 2025 · 4 comments
Open

Usage of maxProperties not possible #1665

bsoaressimoes opened this issue Feb 5, 2025 · 4 comments

Comments

@bsoaressimoes
Copy link

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" ]
}
@unkish
Copy link
Collaborator

unkish commented Feb 11, 2025

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.

@joelittlejohn
Copy link
Owner

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

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.

@bsoaressimoes
Copy link
Author

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.

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

3 participants