Skip to content

[BUG] Incompatible Content type handling leading to NPE #11418

@prezmer

Description

@prezmer

Version 5.2.1 generated Java class ApiClient which contained method

    public String selectHeaderContentType(String[] contentTypes) {
        if (contentTypes.length == 0 || contentTypes[0].equals("*/*")) {
            **return "application/json";**
        }
        for (String contentType : contentTypes) {
            if (isJsonMime(contentType)) {
                return contentType;
            }
        }
        return contentTypes[0];
    }

Version 5.3.x (0 and 1) generates modified body of the method

    public String selectHeaderContentType(String[] contentTypes) {
        if (contentTypes.length == 0) {
            **return null;**
        }

        if (contentTypes[0].equals("*/*")) {
            return "application/json";
        }

        for (String contentType : contentTypes) {
            if (isJsonMime(contentType)) {
                return contentType;
            }
        }

        return contentTypes[0];
    }

Returning null when argument is empty finally causes NPE in existing code. I guess that we should avoid returning as a rule of clean code.

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

NPE thrown in a java client code

openapi-generator version

5.3.x
5.2.x works fine

Suggest a fix

instead of
if (contentTypes.length == 0) {
return null;
}

use
if (contentTypes.length == 0) {
return "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