-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Feature Request: Common Interface for Additional Properties Setter
Summary
When jsonschema2pojo generates a class that includes a setAdditionalProperty method (for handling additionalProperties in JSON Schema), it should also implement a common interface (e.g., HasAdditionalProperties). This interface should define the setAdditionalProperty(String name, Object value) method, making it easier for downstream code to work generically with all such POJOs.
Motivation
Currently, each generated class with additionalProperties defines its own setAdditionalProperty method, but there is no common contract/interface unifying these. This makes it more difficult for library users to write utilities or frameworks that operate generically on all such POJOs.
Proposed Solution
-
Add a new interface,
HasAdditionalProperties, with the following signature:public interface HasAdditionalProperties { void setAdditionalProperty(String name, Object value); }
-
Update the code generation logic (in AdditionalPropertiesRule) so that any generated class containing setAdditionalProperty also implements HasAdditionalProperties.
-
Ensure that the generated setAdditionalProperty method matches the interface signature.
-
Update or add integration tests to verify the interface is implemented where expected.
-
The interface HasAdditionalProperties should also be generated or copied into the same output directory as the generated POJOs. This ensures that user projects do not need to depend on jsonschema2pojo-core at runtime or compile time. The generator should ensure that the interface is only generated/copied once per output directory, to avoid duplicate class errors.
Benefits
- Enables easier generic programming and utility code for jsonschema2pojo users.
- Encourages consistent method signatures for additional properties handling.
- Reduces custom boilerplate for consumers who want to interact with additional properties.
- Eliminates the need for user projects to add jsonschema2pojo-core as a dependency just for this interface.
Notes
- The interface should NOT be generic; the value parameter should be Object, just like the current behavior for additionalProperties by default.
- If a generated POJO narrows the property type (e.g., via a schema), the interface signature should still use Object for compatibility.
- If none of the generated classes implement the method or the feature is deactivated, the interface itself should not be added to the generated code.
Related files
- jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/AdditionalPropertiesRule.java
- jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/HasAdditionalProperties.java (template location for copy)
Thank you for considering this feature!
This issue was drafted with GitHub Copilot.