OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAXRS-SPEC] Schema Mapping Produces Compilation Error; Type Parameter Annotated with @Valid

Open
#20,377 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

The issue happens when an object has a property of type array, and you use a schema mapping for the array type:

    GetItemsResponse:
      type: object
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/Item'
      required:
        - items

--schema-mappings Item=org.example.Item

Actual Output: Here's the line that doesn't compile:

  @NotNull @Valid public List<@Valid org.example.Item> getItems() {

Note: @Valid is a Jakarta annotation here.

Expected Output: The type parameter should not be annotated with @Valid.

Impact: I have a build workflow where I generate Java sources from OpenAPI YAML, and then compile the generated sources. The impact is severe when the code that is generated doesn't compile.

openapi-generator version

7.10.0

OpenAPI declaration file content or url

I've produced a simpler example that reproduces the problem:

openapi: 3.0.0

info:
  title: Test API
  version: 1.0.0

paths:
  /get-items:
    get:
      operationId: getItems
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GetItemsResponse'

components:
  schemas:
    GetItemsResponse:
      type: object
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/Item'
      required:
        - items
    Item:
      type: string

The idea here is that we can use a scheme mapping for Item, but it will default to a string without the mapping.

Generation Details

I'm using the Gradle plugin to generate JAX-RS interfaces, and then I compile the sources. I do use a schema mapping for one type. Here is a simpler build file that reproduces the problem.

build.gradle.kts

plugins {
    `java-library`
    id("org.openapi.generator") version "7.10.0"
}

fun projectFilePath(relativePath: String): Provider<String> =
    layout.projectDirectory.file(providers.provider { relativePath }).map { it.asFile.absolutePath }

fun buildDirPath(relativePath: String): Provider<String> =
    layout.buildDirectory.dir(relativePath).map { it.asFile.absolutePath }

// Validate the OpenAPI YAML.
openApiValidate {
    inputSpec = projectFilePath("src/main/resources/api.yaml")
}

// Generate JAX-RS server interfaces from the OpenAPI YAML.
openApiGenerate {
    generatorName = "jaxrs-spec"
    inputSpec = projectFilePath("src/main/resources/api.yaml")
    schemaMappings = mapOf("Item" to "org.example.Item")
    outputDir = buildDirPath("generated/sources/openApi/java/main")

    // Generate interfaces.
    configOptions = mapOf(
        "interfaceOnly" to "true",
        "sourceFolder" to "",
        "useJakartaEe" to "true",
        "useSwaggerAnnotations" to "false",
    )

    // Generate only the apis and models. Don't generate the invokers or other supporting files.
    apiPackage = "org.example"
    modelPackage = "org.example"
    globalProperties = mapOf(
        "apis" to "",
        "models" to "",
    )
}

// Compile the Java sources that are generated from the OpenAPI YAML.
sourceSets {
    main {
        java {
            srcDir(tasks.named("openApiGenerate"))
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    api(libs.jackson.annotations)
    api(libs.jakartaAnnotation.api)
    api(libs.jakartaValidation.api)
    api(libs.jaxRs.api)
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}
Steps to reproduce
  • Download and extract archive.zip, and run ./gradlew build.
  • If you remove the schemaMappings line from build.gradle.kts, it will build successfully.
Related issues/PRs
  • #17874, though for some reason it's marked as an enhancement, not a bug.
  • I have not extensively checked other Java generators, though java works without error if you use Retrofit + Jackson.
Suggest a fix

The Jakarta @Valid annotation should not be used for a type parameter.

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 supplied archive, build.gradle.kts, and the api.yaml reproduction, then run ./gradlew build using the jaxrs-spec generator with the Item schema mapping. Compare the generated interface with and without schemaMappings. Done means the generated Java sources compile and the mapped array element type is not annotated with Jakarta @Valid.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.