openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA] Enum not generated correctly for contents having common prefix

Open shivam-lal-maersk opened this issue 1 year ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

While generating openapi spec with enums having common prefix, the common prefix is skipped in the enum name. Eg:

ResourceTypeV1:
      type: string
      description: Defines a resource type to provision, it can be multiple resources e.g. DATABASE or TOPIC
      enum:
        - RESOURCE_ACCESS_REQUEST
        - RESOURCE_ACCESS_GRANT

Class generated:

public enum ResourceTypeV1 {
    REQUEST("RESOURCE_ACCESS_REQUEST"),
    GRANT("RESOURCE_ACCESS_GRANT");
  private String value;

Expected class generated:

public enum ResourceTypeV1 {
    RESOURCE_ACCESS_REQUEST("RESOURCE_ACCESS_REQUEST"),
    RESOURCE_ACCESS_GRANT("RESOURCE_ACCESS_GRANT");
  private String value;
openapi-generator version: 7.6.0
OpenAPI declaration file content or url
Generation Details

Using maven to generate. Below is the configuration for the openapi-generator-maven-plugin

<configuration>
  <inputSpec>${project.basedir}/src/main/resources/schema/openapi_rar.yaml</inputSpec>
  <generatorName>spring</generatorName>
  <packageName>com.maersk.relaystream.provisioning.schema.rar</packageName>
  <apiPackage>com.maersk.relaystream.provisioning.generated.api.rar</apiPackage>
  <modelPackage>com.maersk.relaystream.provisioning.generated.model.rar</modelPackage>
  <configOptions>
    <sourceFolder>src/gen/java</sourceFolder>
    <ignoreAnyOfInEnum>true</ignoreAnyOfInEnum>
    <interfaceOnly>true</interfaceOnly>
    <useSpringBoot3>true</useSpringBoot3>
    <openApiNullable>false</openApiNullable>
    <serializableModel>true</serializableModel>
    <containerDefaultToNull>false</containerDefaultToNull>
  </configOptions>
</configuration>
Steps to reproduce
  • Create an OpenAPI spec having the enum mentioned above
  • Generate the class from the spec.
Related issues/PRs
Suggest a fix

shivam-lal-maersk avatar Jun 10 '24 07:06 shivam-lal-maersk

@shivam-lal-maersk there is an undocumented additionalProperty removeEnumValuePrefix.

Be aware for the maven plugin: as it is not documented in cliOptions, you need to use the additionalProperties: <additionalProperties>removeEnumValuePrefix=false</additionalProperties> or

<additionalProperties>
  <additionalProperty>removeEnumValuePrefix=false</additionalProperty>
</additionalProperties>

You can see details in https://github.com/OpenAPITools/openapi-generator/pull/5166

If documented in cliOptions, you could use

<configOptions>
   <removeEnumValuePrefix>false</removeEnumValuePrefix>
</configOptions>

CodegenConstants contains the constants but the description is not used anywhere. So most probably a bug.

    public static final String REMOVE_ENUM_VALUE_PREFIX = "removeEnumValuePrefix";
    public static final String REMOVE_ENUM_VALUE_PREFIX_DESC = "Remove the common prefix of enum values";

I think it is very confusing to have some properties defined in additionalProperties when they should be configOptions. There are several other undocumented properties or only available in the cli:

  • docExtension
  • ignoreFileOverride (--ignore-file-override in the cli)
  • removeEnumValuePrefix
  • removeOperationIdPrefixCount
  • removeOperationIdPrefixDelimiter
  • generatePropertyChanged (only for CSharp)
  • stripPackageName (only for scala)

And probably other

jpfinne avatar Jun 16 '24 13:06 jpfinne

removeEnumValuePrefix is now default to false via https://github.com/OpenAPITools/openapi-generator/pull/20452

wing328 avatar Jan 13 '25 09:01 wing328