Skip to content

[BUG] [JAVA] Invalid default assignments for complex object properties #21051

Closed
@mtraynham

Description

@mtraynham

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

When an OpenAPI spec supplies complex default objects, the Java generator is assigning invalid defaults.

The issue relates to these lines, https://github.com/OpenAPITools/openapi-generator/blob/v7.12.0/modules/openapi-generator/src/main/resources/Java/pojo.mustache#L72-L87

A simple OpenAPI spec could look like the following:

components:
  schemas:
    Test:
      type: object
      properties:
        testEmptyInlineObject:
          type: object
          default: {}

And this generates the following invalid Java default assignment:

private Object testEmptyInlineObject = {};

A full example with other various scenarios, such as references with defaults, can be tested with https://github.com/mtraynham/openapi-generator-java-defaults-bug

I will note that it's not really denoted in the OpenAPI documentation if this is entirely valid - but I haven't seen anything suggesting it's not.

openapi-generator version
  • 7.12.0
  • main
OpenAPI declaration file content or url

https://github.com/mtraynham/openapi-generator-java-defaults-bug/blob/main/openapi.yaml

Generation Details
  • Java
Steps to reproduce
  1. Clone https://github.com/mtraynham/openapi-generator-java-defaults-bug
  2. Run make .generated
Related issues/PRs
Suggest a fix

I suppose the defaults can be removed in these cases, or proper Java objects should be created
from the JSON.

For now, I've opted to provide my own pojo.mustache template which removes default assignments.

74c74
<   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>undefined();
---
>   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} JsonNullable<{{{datatypeWithEnum}}}> {{name}};
77c77
<   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>{{#defaultValue}}of({{{.}}}){{/defaultValue}}{{^defaultValue}}undefined(){{/defaultValue}};
---
>   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} JsonNullable<{{{datatypeWithEnum}}}> {{name}};
82c82
<   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
---
>   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{{datatypeWithEnum}}} {{name}};
85c85
<   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
---
>   {{#hasChildren}}protected{{/hasChildren}}{{^hasChildren}}private{{/hasChildren}} {{{datatypeWithEnum}}} {{name}};

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