OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Java Spring stub generation generates code that does not compile when using callbacks in the api

Open
#15,690 0 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 generation of a Java Spring stub for a json API that contains callbacks seems broken: java code is generated, but does not compile.

openapi-generator version

This is a regression detected in versions 6.4.0 and later.

  • 6.1.0 : OK
  • 6.3.0 : OK
  • 6.4.0 : KO
  • 6.6.0 : KO
  • 7.0.0-SNAPSHOT (latest master from 2023/05/27): KO
OpenAPI declaration file content or url

The bug has been reproduced with the exact content of https://swagger.io/docs/specification/callbacks/ tutorial:

openapi: 3.0.0
info:
  version: 0.0.0
  title: test
paths:
  /subscribe:
    post:
      summary: Subscribe to a webhook
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                callbackUrl:   # Callback URL
                  type: string
                  format: uri
                  example: https://myserver.com/send/callback/here
              required:
                - callbackUrl
      callbacks:   # Callback definition
        myEvent:   # Event name
          '{$request.body#/callbackUrl}':   # The callback URL,
                                            # Refers to the passed URL
            post:
              requestBody:   # Contents of the callback message
                required: true
                content:
                  application/json:
                    schema:
                      type: object
                      properties:
                        message:
                          type: string
                          example: Some event happened
                      required:
                        - message
              responses:   # Expected responses to the callback message
                '200':
                  description: Your server returns this code if it accepts the callback
      responses:
        '201':
          description: Webhook created
Generation Details

Language: Java
Java version: openjdk 17.0.7 2023-04-18

Detailed command line used for stub generation: java -jar openapi-generator-cli-7.0.0-SNAPSHOT.jar generate -g spring -i apis.yaml -o stub-spring -p generateAliasAsModel=false -p returnSuccessCode=true -p artifactId=spring_stub -p artifactVersion="0.0.1"

Steps to reproduce
  • Execute openapi-generator-cli with command line
    java -jar openapi-generator-cli-6.6.0.jar generate -g spring -i apis.json -o stub-spring -p generateAliasAsModel=false -p returnSuccessCode=true -p artifactId=spring_stub -p artifactVersion="0.0.1"

=> The generation produces code , but also emits warnings that looks related:

[main] WARN  o.o.codegen.DefaultCodegen - Empty operationId found for path: post /subscribe. Renamed to auto-generated operationId: subscribePost
[main] WARN  o.o.codegen.DefaultCodegen - Empty operationId found for path: post myEvent_. Renamed to auto-generated operationId: myEvent_Post
[main] WARN  o.o.codegen.DefaultCodegen - The following schema has undefined (null) baseType. It could be due to form parameter defined in OpenAPI v2 spec with incorrect consumes. A correct 'consumes' for form parameters should be 'application/x-www-form-urlencoded' or 'multipart/?'
[main] WARN  o.o.codegen.DefaultCodegen - schema: class ObjectSchema {
    class Schema {
        type: object
        format: null
        $ref: null
        description: null
        title: null
        multipleOf: null
        maximum: null
        exclusiveMaximum: null
        minimum: null
        exclusiveMinimum: null
        maxLength: null
        minLength: null
        pattern: null
        maxItems: null
        minItems: null
        uniqueItems: null
        maxProperties: null
        minProperties: null
        required: [callbackUrl]
        not: null
        properties: {callbackUrl=class StringSchema {
            class Schema {
                type: string
                format: uri
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: https://myserver.com/send/callback/here
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }}
        additionalProperties: null
        nullable: null
        readOnly: null
        writeOnly: null
        example: null
        externalDocs: null
        deprecated: null
        discriminator: null
        xml: null
    }
}
[main] WARN  o.o.codegen.DefaultCodegen - codegenModel is null. Default to UNKNOWN_BASE_TYPE
  • Try packaging with maven: cd stub-spring && mvn clean package

=> Compilation fails with following errors:

[ERROR] COMPILATION ERROR : 
[ERROR] stub-spring/src/main/java/org/openapitools/api/SubscribeApi.java:[8,30] cannot find symbol
  symbol:   class UNKNOWN_BASE_TYPE
  location: package org.openapitools.model
[ERROR] stub-spring/src/main/java/org/openapitools/api/SubscribeApi.java:[64,103] cannot find symbol
  symbol:   class UNKNOWN_BASE_TYPE
  location: interface org.openapitools.api.SubscribeApi
[ERROR] stub-spring/src/main/java/org/openapitools/api/SubscribeApiController.java:[3,30] cannot find symbol
  symbol:   class UNKNOWN_BASE_TYPE
  location: package org.openapitools.model
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project spring_stub: Compilation failure: Compilation failure: 
[ERROR] stub-spring/src/main/java/org/openapitools/api/SubscribeApi.java:[8,30] cannot find symbol
[ERROR]   symbol:   class UNKNOWN_BASE_TYPE
[ERROR]   location: package org.openapitools.model
[ERROR] stub-spring/src/main/java/org/openapitools/api/SubscribeApi.java:[64,103] cannot find symbol
[ERROR]   symbol:   class UNKNOWN_BASE_TYPE
[ERROR]   location: interface org.openapitools.api.SubscribeApi
[ERROR] stub-spring/src/main/java/org/openapitools/api/SubscribeApiController.java:[3,30] cannot find symbol
[ERROR]   symbol:   class UNKNOWN_BASE_TYPE
[ERROR]   location: package org.openapitools.model
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Related issues/PRs

None identified (perhaps #800 ?)

Suggest a fix

Produced code should compile successfully ("BUILD SUCCESS")

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 openapi-generator CLI using the callback specification, then inspect the generated SubscribeApi.java and SubscribeApiController.java files for the UNKNOWN_BASE_TYPE references. Run mvn clean package in the generated stub and consider the issue complete when the Spring stub builds successfully without those compilation errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.