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

additionalProperties not correctly handled #1662

Open
bradzacher opened this issue Jan 14, 2025 · 5 comments
Open

additionalProperties not correctly handled #1662

bradzacher opened this issue Jan 14, 2025 · 5 comments

Comments

@bradzacher
Copy link

bradzacher commented Jan 14, 2025

I'm trying to generate types for the SARIF JSON Schema spec - https://json.schemastore.org/sarif-2.1.0.json

In particular they have defined:

{
  "originalUriBaseIds": {
    "description": "The artifact location specified by each uriBaseId symbol on the machine where the tool originally ran.",
    "type": "object",
    "additionalProperties": {
      "$ref": "#/definitions/artifactLocation"
    }
  },
}

I would have expected that this would generate a class with additionalProperties as mentioned in the wiki, like:

public class OriginalUriBaseIds {
  private java.util.Map<String, ArtifactLocation> additionalProperties = new java.util.HashMap<String, ArtifactLocation>();
  
  // ...
}

However instead this generates a class without additionalProperties:

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@JsonPropertyOrder({

})
public class OriginalUriBaseIds {


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(OriginalUriBaseIds.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
        if (sb.charAt((sb.length()- 1)) == ',') {
            sb.setCharAt((sb.length()- 1), ']');
        } else {
            sb.append(']');
        }
        return sb.toString();
    }

    @Override
    public int hashCode() {
        int result = 1;
        return result;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof OriginalUriBaseIds) == false) {
            return false;
        }
        OriginalUriBaseIds rhs = ((OriginalUriBaseIds) other);
        return true;
    }

}

This of course causes a crash when deserialising an object that uses the map

Is this a bug in the codegen, or am I "holding it wrong"?

@unkish
Copy link
Collaborator

unkish commented Jan 18, 2025

Hi

If you're using CLI to generate pojos - then please add -D or --enable-additional-properties as an additional command line argument

    -D, --enable-additional-properties
      Enable additional properties support on generated types, regardless of
      the input schema(s)
      Default: false

Ie.

jsonschema2pojo -p "com.example.package" -s sarif-2.1.0.json -t output_dir -D

@bradzacher
Copy link
Author

regardless of the input schema(s)

I tried that but this is a problematic part of the CLI flag. I don't want it on all objects - just the ones with additionalProperties defined.

@unkish
Copy link
Collaborator

unkish commented Jan 19, 2025

From what can be seen in sarif-2.1.0.json it seems that all objects that are not supposed to have additionalProperties are marked with "additionalProperties": false,

I guess that description in GenerationConfig is a tiny bit better:

  /**
   * Gets the 'includeAdditionalProperties' configuration option.
   *
   * @return Whether to allow 'additional properties' support in objects.
   *         Setting this to false will disable additional properties support,
   *         regardless of the input schema(s).
   */

We can also check the source code

If the flag is not enabled then regardless of schema additionalProperties won't be generated for object(s).

Some possible workarounds:

  • pre-process json schema prior passing it to generator
  • create a rulefactory that would return customized variant of AdditionalPropertiesRule such that would bail-out if additionalProperties is null or not defined

@bradzacher
Copy link
Author

I'm confused - your wiki states with examples that you natively support additionalProperties. Is this not the case then?

As mentioned in the OP I was expecting that an object that is only declared with additionalProperties should generate the relevant code.

@unkish
Copy link
Collaborator

unkish commented Jan 19, 2025

As mentioned in the OP I was expecting that an object that is only declared with additionalProperties should generate the relevant code.

Unfortunatelly that is not supported without enabling additional properties support (which in turn would generate additional properties for objects that do not explicitly specify "additionalProperties": false)
As mentioned above there are workarounds to make this happen.

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

2 participants