OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] allOf with just one element generates incorrect validateJsonElement

Open
#19,866 2 comments 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

When I provide only one element in allOf, then autogenerated java code doesn't compile, because it calls validateJsonElement on Object class.

openapi-generator version

7.9.0

OpenAPI declaration file content or url

Below yaml with only one element in allOf:

openapi: 3.1.0
info:
  title: Product API
  version: 1.0.0
paths:
  /product:
    get:
      summary: Get a product
      operationId: getProduct
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Product'

components:
  schemas:
    Product:
      type: object
      title: Product
      required:
        - product
      properties:
        product:
          $ref: '#/components/schemas/productDetails'
      additionalProperties: false

    productDetails:
      type: object
      allOf:
        - required:
            - productName
      properties:
        productName:
          type: string

generates such code

      // check to make sure all required properties/fields are present in the JSON string
      for (String requiredField : Product.openapiRequiredFields) {
        if (jsonElement.getAsJsonObject().get(requiredField) == null) {
          throw new IllegalArgumentException(String.format("The required field `%s` is not found in the JSON string: %s", requiredField, jsonElement.toString()));
        }
      }
        JsonObject jsonObj = jsonElement.getAsJsonObject();
      // validate the required field `product`
      Object.validateJsonElement(jsonObj.get("product"));
     //^Cannot resolve method 'validateJsonElement' in 'Object' <-- error I see form above line in my IDE
  }

but when I add second element to allOf, for example:

      allOf:
        - required:
            - productName
        - format: uri

it works fine and java code looks like this:

      // check to make sure all required properties/fields are present in the JSON string
      for (String requiredField : Product.openapiRequiredFields) {
        if (jsonElement.getAsJsonObject().get(requiredField) == null) {
          throw new IllegalArgumentException(String.format("The required field `%s` is not found in the JSON string: %s", requiredField, jsonElement.toString()));
        }
      }
        JsonObject jsonObj = jsonElement.getAsJsonObject();
      // validate the required field `product`
      ProductDetails.validateJsonElement(jsonObj.get("product"));
Generation Details

I use java and gradle and this is my gradle task to generate docs:

tasks.named("openApiGenerate", org.openapitools.generator.gradle.plugin.tasks.GenerateTask::class) {
    generatorName.set("java")
    inputSpec.set("$rootDir/openapi/bundled.yaml")
    outputDir.set("$buildDir/generated")
    apiPackage.set("com.yourcompany.api")
    modelPackage.set("com.yourcompany.model")
    invokerPackage.set("com.yourcompany.invoker")
    configOptions.set(mapOf(
        "dateLibrary" to "java8",
        "java8" to "true"
    ))
}
Steps to reproduce

just run openApiGenerate with provided openapi.
Expected behaviour is that generated code compiles and work fine even if there is only one element in allOf.

Related issues/PRs
Suggest a 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 the Java generator invoked by the openApiGenerate Gradle task and reproduce the issue using the minimal OpenAPI YAML in the report. Compare generated validation for a one-element allOf with the two-element case; done means the generated Java code references the model type correctly and compiles.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.