Skip to content

[BUG][Java] Generated API class has wrong dataType and does not compile #5324

@sebastien-rosset

Description

@sebastien-rosset

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Using OpenAPI v3 document to generate a client-side Java SDK with the default library (okhttp-gson). The generated classes under the "api" directory cause a compilation failure.

For example, a generated API Java class api/SyslogApi.java has the following createSyslogPolicyCall() method. The requestBody argument is a templatized PolicyAbstractPolicy<String, Object>.

  public okhttp3.Call
  createSyslogPolicyCall(PolicyAbstractPolicy<String, Object> requestBody,
                         final ApiCallback _callback) throws ApiException {
    Object localVarPostBody = requestBody;

However, the generated model/PolicyAbstractPolicy.java class is not a generic, hence the compilation fails. There are two problems:

  1. The argument uses a generic
  2. The argument type is the parent class

I was expecting the generated method signature to be something like:

public okhttp3.Call createSyslogPolicyCall(SyslogPolicy syslogPolicy, final ApiCallback _callback)

The mustache templates that are used to generate these classes are:

  1. API: Java/libraries/native/api.mustache
  2. Model: modules/openapi-generator/src/main/resources/Java/pojo.mustache

It looks like the problem is the value of dataType in the API template does not match the class name of the POJO

{{{dataType}}} {{paramName}}

I am using the default Java library, which is okhttp-gson.

openapi-generator version

master February 14th 2020

OpenAPI declaration file content or url

Operations:

  /syslog/Policies:
    post:
      tags:
      - syslog
      summary: Create a 'syslog.Policy' resource.
      operationId: CreateSyslogPolicy
      requestBody:
        description: The 'syslog.Policy' resource to create.
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/syslog.Policy'
      responses:
        '200':
          description: The 'syslog.Policy'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/syslog.Policy'

Schema:

    mo.BaseMo:
      type: object
      discriminator:
        propertyName: objectType
      required:
      - objectType
      properties:
        objectType:
          type: string

    policy.AbstractPolicy:
      allOf:
      - $ref: '#/components/schemas/mo.BaseMo'
      - type: object
        required:
        - objectType
        properties:
          description:
            type: string

    syslog.Policy:
      additionalProperties: {}
      allOf:
      - $ref: '#/components/schemas/policy.AbstractPolicy'
      - type: object
        required:
        - objectType
        properties:
          localClients:
            type: array
            items:
              type: string
Command line used for generation
Steps to reproduce

Edit modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml and add the YAML snippets listed in this issue.

Invoke bin/java-petstore-native.sh


Run mvn package in sample directory.

Open samples/......./openapitools/client/api/SyslogApi.java in file editor.

Related issues/PRs
Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions