OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.56k stars 6.28k forks source link

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

Open ondrej-simon opened 2 weeks ago

ondrej-simon commented 2 weeks ago
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")) {