OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Java imports for allOf schema in parameter broken in 6.0.0

Open
#12,690 1 comment 0 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

With version 6.0.0 there came a change in handling imports for operation parameter types in generated API classes. If there is an parameter with schema that uses allOf, the "parent" class will be incorrectly imported instead of the actual class.

Version 5.4.0:

package org.openapitools.api;

...
import org.openapitools.model.BuggedParameterType;
...

@Client("${base-path}")
public interface BugApi {

    @Get(uri="/test/path")
    @Consumes(value={"application/json"})
    Mono<Object> buggedOperation(
        @QueryValue(value="buggedParameter") @NotNull @Valid BuggedParameterType buggedParameter
  );
}

Version 6.0.0:

package org.openapitools.api;

...
import org.openapitools.model.BuggedParameterBaseType;
...

@Client("${base-path}")
public interface BugApi {

    @Get(uri="/test/path")
    @Consumes(value={"application/json"})
    Mono<Object> buggedOperation(
        @QueryValue(value="buggedParameter") @NotNull @Valid BuggedParameterType buggedParameter
  );
}

This leads to compile error, since the actual parameter type is not imported.

This example is Java Micronaut output, but I'm quite sure this is applies to generic Java output as well.

I created a repository for easier issue reproduction here: https://github.com/oujesky/openapi-generator-java-import-bug-repro-steps

openapi-generator version

5.4.0 - OK
6.0.0 - incorrect

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Bug report
  version: '1.0'
paths:
  /test/path:
    get:
      tags:
        - Bug
      operationId: buggedOperation
      parameters:
        - name: buggedParameter
          in: query
          required: true
          explode: true
          schema:
            $ref: '#/components/schemas/BuggedParameterType'
      responses:
        200:
          description: OK
components:
  schemas:
    BuggedParameterBaseType:
      type: object
      properties:
        x:
          type: string
        y:
          type: string
    BuggedParameterType:
      allOf:
        - $ref: '#/components/schemas/BuggedParameterBaseType'
        - type: object
          properties:
            z:
              type: string
Generation Details

Default settings for java-micronaut-client (but should be reproducible for other outputs as well)

openApiGenerate {
    inputSpec = "$rootDir/openapi.yml"
    generatorName = 'java-micronaut-client'
    outputDir = "$buildDir/openapi"
}
Steps to reproduce
Related issues/PRs
  • #12369
  • #12724
Suggest a fix

I was able to somewhat pinpoint the place where this is happening to changes coming from #12369 - specifically changes in DefaultCodegen#fromParameter(...) method (https://github.com/OpenAPITools/openapi-generator/pull/12369/files#diff-3a138675e8cb40943bcb00feb46edd6f6ee5c5306c29cc19f02c845a299c8658L4580) that incorrectly returns information about the "parent" class, instead of the actual class that is then added to imports.

Unfortunately I'm not proficient with the code enough to suggest an actual fix.

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 with DefaultCodegen#fromParameter and compare the import-related changes associated with #12369. Reproduce the issue using the linked repository and ./gradlew openApiGenerate, then compare the generated BugApi.java files for versions 5.4.0 and 6.0.0. Done means the generated Java API imports BuggedParameterType rather than BuggedParameterBaseType and no longer has the reported compile error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.