OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java][native] Regression Native library generator cannot handle model with unique enum field

Open
#17,004 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

Description

When updating the gradle plugin from version 6.2.1 to 7.0.1 the generated code no longer compiles.
relevant stuff from the schema is:

          "operators": {
            "items": {
              "enum": [
                "EQUALS",
                "NOT_EQUALS",
                "GREATER_OR_EQUAL",
                "GREATER",
                "LESSER_OR_EQUAL",
                "LESSER",
                "STARTS_WITH",
                "NOT_STARTS_WITH",
                "ENDS_WITH",
                "NOT_ENDS_WITH",
                "EMPTY",
                "NOT_EMPTY",
                "CONTAINS",
                "NOT_CONTAINS"
              ],
              "type": "string"
            },
            "type": "array",
            "uniqueItems": true
          },

This is from a model definition.
The generated code that does not compile looks like this:

    // add `operators` to the URL query string
    if (getOperators() != null) {
      int i = 0;
      for (String _item : getOperators()) {
        joiner.add(String.format("%soperators%s%s=%s", prefix, suffix,
            "".equals(suffix) ? "" : String.format("%s%d%s", containerPrefix, i, containerSuffix),
            URLEncoder.encode(String.valueOf(_item), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
      }
      i++;
    }

However get operators looks like this:

  public Set<OperatorsEnum> getOperators() {
    return operators;
  }

This obvieusly does not compile since OperatorsEnum != String

This entire thing only exists in context of toUrlQueryString which is very confusing as I use JSON for this model
and never URLQuery. My editor also tells me that the new method toUrlQueryString is never used.

When manually chaning the defition of the model to be "uniqueItems": false the generated code does compile.
This is however not an option for me as I am not the "source" of the openapi files it take them from another vendor.

openapi-generator version

It is a regression!

OpenAPI declaration file content or url
Generation Details

tasks.register("genV2") {
validateSpec.set(false)
generatorName.set("java")
inputSpec.set("$rootDir/src/main/resources/redacted.json")
library.set("native")
outputDir.set("$rootDir/generated/v2")
invokerPackage.set("redacted")
modelPackage.set("redacted")
apiPackage.set("redacted")
configOptions.put("dateLibrary", "java8")
configOptions.put("serializableModel", "true")
configOptions.put("authorization", "basicAuth")
configOptions.put("useGenericResponse", "true")
configOptions.put("artifactId", "tmp")
configOptions.put("groupId", "redacted")
configOptions.put("basePackage", "redacted")
configOptions.put("configPackage", "redacted")
configOptions.put("useRuntimeException", "true")
configOptions.put("hideGenerationTimestamp", "true")
}

Steps to reproduce

See above

Related issues/PRs

None to my knowledge

Suggest a fix

Do not generate the toUrlQueryString or let me have a configOptions in my build script so i can disable this generation. I dont need it.

EDIT: i just realized that such an option already exists and solves my problem adding:
configOptions.put("supportUrlQuery", "false")
makes the compile error go away. Still for those that do need this option it should still be fixed.

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

Reproduce the issue with the Gradle genV2 task using the Java generator's native library and a schema containing a unique enum array. Inspect the generated model's toUrlQueryString method and the handling of getOperators(); done means generated code compiles while preserving URL query support for the set of enum values.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.