Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ondrej-simon opened this issue Jun 14, 2024 · 0 comments · May be fixed by #18930
Open

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

ondrej-simon opened this issue Jun 14, 2024 · 0 comments · May be fixed by #18930

Comments

@ondrej-simon
Copy link

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")) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant