OpenAPITools / OpenAPITools/openapi-generator

[JavaJaxRS][resteasy] enum value starting with a digit

Open
#4,692 0 comments 0 reactions 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)?
  • 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

Enum value starting with a digit is marshalled/unsmarshalled in request/response body with a leading underscore character. It is also true when the enum type is a query param.
Example: enum value 5G_EIR is transformed into _5G_EIR in the code and is marshalled/unmarshalled as _5G_EIR. It should be 5G_EIR.

openapi-generator version

4.0.2

OpenAPI declaration file content or url

In file TS29510_Nnrf_NFManagement.yaml, the NFType defintion is the following one:

    NFType:
      type: string
      enum:
        - NRF
        - UDM
        - AMF
        - SMF
        - AUSF
        - NEF
        - PCF
        - SMSF
        - NSSF
        - UDR
        - LMF
        - GMLC
        - 5G_EIR
        - SEPP
        - UPF
        - N3IWF
        - AF
        - UDSF
        - BSF
        - CHF
        - NWDAF

Then the generated java source code is :

package org.openapitools.model;

import java.util.Objects;
import java.util.ArrayList;
import com.fasterxml.jackson.annotation.JsonValue;
import javax.validation.constraints.*;
public enum NFType {
    NRF, UDM, AMF, SMF, AUSF, NEF, PCF, SMSF, NSSF, UDR, LMF, GMLC, _5G_EIR, SEPP, UPF, N3IWF, AF, UDSF, BSF, CHF, NWDAF
}
Command line used for generation
java -jar ./openapi-generator-cli-4.0.2.jar generate -i TS29510_Nnrf_NFManagement.yaml -g jaxrs-resteasy -o generated-files
Steps to reproduce

Please unzip the attached file and type the command (see above) in the directory where files are stored.
Looks for the generated java enum NFType file in the generated-files directory.

Related issues/PRs

#1051

Suggest a fix

The suggested fix is the one we applied in our project without upgrading the generator version.

The issue occurs at 3 levels:

  • request/response body,
  • query param,
  • swagger.json generation (swagger annotations)

For request/response body, the JavaJaxRS enumClass.mustache and enumOuterClass.mustache template files already contain the fix we needed. So we just copied these files into the directory resteasy.
The fix worked well for the NFType enum value 5G_EIR and of course for any other value of this enum.
Unfortunately, we found an issue with the enum type ServiceName which was generated like this

public enum ServiceName {
  
  NNRF_NFM("nnrf-nfm"),
  
  NNRF_DISC("nnrf-disc"),
  
  NUDM_SDM("nudm-sdm"),
  
  NUDM_UECM("nudm-uecm"),
  
  NUDM_UEAU("nudm-ueau"),
  
  NUDM_EE("nudm-ee"),

  NUDM_PP("nudm-pp"),

  NAMF_COMM("namf-comm"),
  
  NAMF_EVTS("namf-evts"),
  
  NAMF_MT("namf-mt"),
  
  NAMF_LOC("namf-loc"),
  
  NSMF_PDUSESSION("nsmf-pdusession"),
  
  NSMF_EVENT_EXPOSURE("nsmf-event-exposure"),
  
  NAUSF_AUTH("nausf-auth"),
  
  NAUSF_SORPROTECTION("nausf-sorprotection"),
  
  NNEF_PFDMANAGEMENT("nnef-pfdmanagement"),
  
  NPCF_AM_POLICY_CONTROL("npcf-am-policy-control"),
  
  NPCF_SMPOLICYCONTROL("npcf-smpolicycontrol"),
  
  NPCF_POLICYAUTHORIZATION("npcf-policyauthorization"),
  
  NPCF_BDTPOLICYCONTROL("npcf-bdtpolicycontrol"),
  
  NPCF_EVENTEXPOSURE("npcf-eventexposure"),
  
  NPCF_UE_POLICY_CONTROL("npcf-ue-policy-control"),
  
  NSMSF_SMS("nsmsf-sms"),
  
  NNSSF_NSSELECTION("nnssf-nsselection"),
  
  NNSSF_NSSAIAVAILABILITY("nnssf-nssaiavailability"),
  
  NUDR_DR("nudr-dr"),
  
  NLMF_LOC("nlmf-loc"),
  
  N5G_EIR_EIC("n5g-eir-eic"),
  
  NBSF_MANAGEMENT("nbsf-management"),
  
  NCHF_SPENDINGLIMITCONTROL("nchf-spendinglimitcontrol"),
  
  NCHF_CONVERGEDCHARGING("nchf-convergedcharging"),
  
  NNWDAF_EVENTSSUBSCRIPTION("nnwdaf-eventssubscription"),
  
  NNWDAF_ANALYTICSINFO("nnwdaf-analyticsinfo");

  private String value;

  ServiceName(String value) {
	this.value = value;
  }

  @Override
  @JsonValue
  public String toString() {
    return String.valueOf(value);
  }

  @JsonCreator
  public static ServiceName fromValue(String value) {
    for (ServiceName b : ServiceName.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    throw new IllegalArgumentException("Unexpected value '" + value + "'");
  }
}

As we can see, any string value is in lowercase instead to be as defined in the yam file (in our case in uppercase). Moreover the underscore character is replaced by a minus character.
May be this problem comes from the core layer of the generator.

So we fixed the template files by replacing the piece of code about the constructor:

  {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
    this.value = value;
  }

by

  {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
    final String prefix = "_";
	final String name = name();
	this.value = name.startsWith(prefix) ? name.substring(prefix.length()) : name;
  }

We use the name of the enum to set the value, no more the one passed as argument.

For the query param (and other kind of param annotations), we wrote a ParamConverterProvider based on this gist
https://gist.github.com/nickbabcock/d34c360e7af44be4f8ed142c14e143cc .
Thanks @nickbabcock

For the swagger.json file, we didn't fix it.

We hope this will help..

issue-4692.zip

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 jaxrs-resteasy enumClass.mustache and enumOuterClass.mustache templates, then reproduce the issue with the supplied TS29510_Nnrf_NFManagement.yaml and the 4.0.2 generation command. Check generated body and parameter handling, plus swagger.json generation; done means enum values such as 5G_EIR and n5g-eir-eic retain their declared wire values across these outputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.