openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][Java][Spring] Missing JsonIgnoreProperties import when using oneOf within oneOf

Open ondrej-simon opened this issue 2 years ago • 3 comments

Description

When using oneOf for request of response schema, and the specialization contains oneOf as its property, the @JsonIgnoreProperties annotation is added but its import is not. Because of this, the source code cannot be compiled.

openapi-generator version

7.7.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: oneOf within oneOf
  version: 7.7.0-SNAPSHOT
paths:
  /oneOf-within-oneOf-test:
    post:
      operationId: 'oneOfWithinOneOfTest'
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/RequestTwoSchema'
                - $ref: '#/components/schemas/RequestOneSchema'
      responses:
        '201':
          description: The request was successful and the resource has been created.
          content:
            application/json:
              schema:
                type: string
components:
  schemas:
    AbstractNestedSchema:
      type: object
      discriminator:
        propertyName: type
        mapping:
          NESTED_ONE: '#/components/schemas/NestedOneSchema'
          NESTED_TWO: '#/components/schemas/NestedTwoSchema'
      required:
        - type
      properties:
        type:
          type: string
    NestedOneSchema:
      type: object
      allOf:
        - $ref: '#/components/schemas/AbstractNestedSchema'
    AbstractRequestSchema:
      type: object
      discriminator:
        propertyName: type
        mapping:
          REQUEST_ONE: '#/components/schemas/RequestOneSchema'
          REQUEST_TWO: '#/components/schemas/RequestTwoSchema'
      required:
        - type
      properties:
        type:
          type: string
    RequestTwoSchema:
      type: object
      allOf:
        - $ref: '#/components/schemas/AbstractRequestSchema'
      required:
        - nested
      properties:
        nested:
          oneOf:
            - $ref: '#/components/schemas/NestedTwoSchema'
            - $ref: '#/components/schemas/NestedOneSchema'
    RequestOneSchema:
      type: object
      allOf:
        - $ref: '#/components/schemas/AbstractRequestSchema'
    NestedTwoSchema:
      type: object
      allOf:
        - $ref: '#/components/schemas/AbstractNestedSchema'

Generation Details

Generation was performed on the master branch of the openapi-generator project, pulled directly from this GitHub repository.

Steps to reproduce
  1. Copy the yaml described above into modules/openapi-generator/src/test/resources/3_0/spring/oneof-within-oneof.yaml,
  2. Add the following failing test to org.openapitools.codegen.java.spring.SpringCodegenTest, verifying the import is not added.
@Test
public void shouldAddJsonIgnorePropertiesAnnotationWhenUsingOneOfWithinOneOf() throws IOException {
    final SpringCodegen codegen = new SpringCodegen();

    final Map<String, File> files = generateFiles(codegen, "src/test/resources/3_0/spring/oneof-within-oneof.yaml");

    JavaFileAssert.assertThat(files.get("RequestTwoSchemaNestedOneOf.java"))
            .hasImports("com.fasterxml.jackson.annotation.JsonIgnoreProperties");
}
Suggest a fix

The problem is within the following method: org.openapitools.codegen.languages.AbstractJavaCodegen#addImportsToOneOfInterface, where the import request for JsonIgnoreProperties is missing.

Incorrect:

for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo")) {

Fixed version:

for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo", "JsonIgnoreProperties")) {

ondrej-simon avatar Jun 14 '24 12:06 ondrej-simon

Hi @ondrej-simon I have tried this with the latest version 7.7.0 and in my case the import com.fasterxml.jackson.annotation.JsonIgnoreProperties was there.

sonerd avatar Jul 10 '24 16:07 sonerd

I am closing this issue, because in the end I found out what I was trying to achieve was not optimal. It is much better to explicitly define a customer schema with type: object, acting as a definition for oneOf possibilities. In such case, import generation works just fine.

ondrej-simon avatar Apr 30 '25 12:04 ondrej-simon

I'm running into this bug with my OpenAPI spec file so it seems that this is still happening. Could this bug be re-opened?

DennisPallett avatar Jan 08 '26 12:01 DennisPallett