OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] emitting non-compilable code with map&&!container

Open
#13,551 2 comments 1 reaction 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

Don't have a minimized reproduction example yet, but it is triggered on recursive syntax tree schema on 5.x+ openapi-generator (also latest 6.2.0).
Following part of template in java pojo.mustache

  {{#isMap}}

  public {{classname}} put{{nameInCamelCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) {
    {{#vendorExtensions.x-is-jackson-optional-nullable}}
    if (this.{{name}} == null || !this.{{name}}.isPresent()) {
      this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}});
    }

emits code like

  public SyntaxTree putNotItem(String key,  notItem) {
    this.not.put(key, notItem);
    return this;
  }

notice that items.datatypeWithEnum is empty and notItem breaks compilation.
Adding {{#isContainer}} in addition to {{#isMap}} fixes it by not emitting a wrong method (setNot method is the only one needed and it is still emitted)

openapi-generator version

6.2.0, seems to have started since 5.x

OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

Not sure if isMap&&!isContainer a valid combination. Fix could either to update template as mentioned above or fixing isMap detection

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

Start with the Java pojo.mustache template and reproduce the issue using a recursive syntax tree schema. Inspect the isMap and isContainer conditions around the map item method, then verify that generated Java no longer contains an empty item type and compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.