OpenAPITools / OpenAPITools/openapi-generator

[JAVA][SPRING] Serialization of generated model shows duplicated properties

Open
#10,884 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The serialization of generated model object contains duplicated properties.

openapi-generator version

5.3.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: test
  version: 0.0.1

paths:
  /dummy:
    description: dummy

components:
  schemas:
    Dummy:
      properties:
        FOO:
          type: string
        BAR:
          type: string
Generation Details
docker run --rm --user=$(id -u) -v $PWD:/local openapitools/openapi-generator-cli generate -i /local/openapi.yaml -g spring  -o /local/out
Steps to reproduce
  1. Run the command above to generate a Maven project that contains the model class Dummy.java
  2. Use ObjectMapper to serialize an instnace of that class:
package org.openapitools.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DummyTest {
    

    @Test
    void test() throws JsonProcessingException {

        ObjectMapper om = new ObjectMapper();

        Dummy d = new Dummy();

        String json = om.writeValueAsString(d);

        System.out.println(json);
    }
}

Result Output:

{
    "bar": null,
    "foo": null,
    "FOO": null,
    "BAR": null
}

Expected Output:

{
    "FOO": null,
    "BAR": null
}
Related issues/PRs

The comments in the following issues suggest that this serialization behavior is actually by design:

https://github.com/FasterXML/jackson-databind/issues/1701
https://github.com/FasterXML/jackson-databind/issues/1609

This post suggests the right way to strucutre the model class to avoid this issue behavior:

https://stackoverflow.com/questions/13262662/duplicate-json-property-when-converting-java-object-to-json-string-using-jackson

Suggest a fix

The post suggests that an annotation should be placed on top of the class to tell the library what to serialize and what not:

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)

Thus there should be an option in the Java/Spring generator to specify such annotation.

As we heavily depends on OpenAPI Generator (thanks for the hard work!), this is blocking us from releasing our API endpoint to customer.

Appreciated.

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

Start by running the provided Docker generation command with the YAML spec, then inspect the generated Dummy.java model and reproduce serialization with the ObjectMapper test shown. Compare the output with the expected JSON and trace how the Spring generator defines model properties and annotations. Done means generated models serialize each property only once, with the requested annotation behavior or configuration clearly verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, tooling
Issue type
Bug
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.