Skip to content

[BUG] [Java] Empty localVarContentTypes in PUT requests with no requestBody leads to exception #12143

@panargirakis

Description

@panargirakis

Bug Report Checklist

Description

The generated code for PUT requests with an empty request body raise a null pointer exception because the localVarContentTypes variable is generated with no default value.

In detail:

My schema specifies PUT requests that do not have a request body. For example:

/api/reservations/:
  /api/reservations/{id}/:
    put:
      operationId: reservations_update
      description: Check queue status or renew lease if reservation is ongoing
      parameters:
      - in: path
        name: id
        schema:
          type: string
          format: uuid
        description: A UUID string identifying this reservation session.
        required: true
      tags:
      - reservations
      security:
      - cookieAuth: []
      - tokenAuth: []
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ReservationSessionSerializerReadOnly'
          description: ''

The generated method does not specify a default Content-Type, like so:

public okhttp3.Call reservationsUpdateCall(UUID id, final ApiCallback _callback) throws ApiException {
    [...]
    final String[] localVarContentTypes = {
    
    };
    [...]
}
openapi-generator version

I generated using docker images. Both openapitools/openapi-generator-cli:v5.4.0 and openapitools/openapi-generator-cli:v6.0.0-beta exhibit this issue.

The schema is validated through apitools.dev.

OpenAPI declaration file content or url

Full schema here

Generation Details

Using the following docker-compose configuration:

version: '3'

services:
  openapi-generator-cli:
    image: openapitools/openapi-generator-cli:v5.4.0
    volumes:
      - ./:/local

Run the following script:

mkdir "temp"
curl https://gist.githubusercontent.com/panargirakis/0d465f954f2c913ef3fc379f1b63da91/raw/0135da54dc950da698e8104bad88ed52af5a6aa6/ResSystem_Schema_2.1.0.yml -o ./temp/latest_schema.yml
docker-compose run --rm openapi-generator-cli generate -i /local/temp/latest_schema.yml -g java -o /local/
Steps to reproduce
  1. Generate using the steps above
  2. The following methods will be missing a value in localVarContentTypes:
    a. DevicesApi:devicesReleaseUpdateCall
    b. GroupsApi:groupsReserveByNameUpdateCall
    c. ReservationsApi:reservationsUpdateCall
    d. and several more
Related issues/PRs

None I am aware of.

Suggest a fix

Simply adding a value in localVarContentTypes fixes the issue. For example, the original example works fine after adding "application/json":

public okhttp3.Call reservationsUpdateCall(UUID id, final ApiCallback _callback) throws ApiException {
    [...]
    final String[] localVarContentTypes = {
        "application/json"
    };
    [...]
}

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