OpenAPITools / OpenAPITools/openapi-generator

[REQ][JAXRS] Support JsonAnyGetter and JsonAnySetter with JAXRS generator to handle unknown properties

Open
#16,172 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

When using models with inheritance, we can support the following use case with JaxRS:

  • Cat inherits from Pet.
  • Cat defines additional properties
  • My application PetStore handles Pets
  • I can send Cats to the application API, with the condition that the Ignore unknown fields in the Mapper is set to true.
  • The application is manipulating it as a Pet, and all unknown properties are gathered by the JsonAnySetter.
  • When serializing the object again (to store in database or send in the API response), the additional properties are added again via the JsonAnyGetter.

This way, we can seamlessly reuse code for an API defined for the parent, using the child model.

Describe the solution you'd like

I would like to have the possibility to generate, for JaxRS spec models, a map of unknown properties to access the properties that could not be parsed.

    private Map<String, Object> unparsedProperties = new HashMap<>();

   @JsonAnySetter
    public void addUnparsedProperty(String property, Object value){
        unparsedProperties.put(property, value);
    }

    @JsonAnyGetter
    public Map<String, Object> getUnparsedProperties(){
        return unparsedProperties;
    }

This should probably be generated only in the parent in the top of the inheritance chain, so just customizing the model template would not be enough.

For the generator option, would it be better to group that feature in a single option, or let the users generate the @JsonAnySetter and @JsonAnyGetter independently ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points. Start by tracing JAXRS spec model generation and inheritance handling, then determine where the unknown-property map and its annotations belong in the top-level generated model. Done means the generator has a defined option and produces the requested behavior for inherited models.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.