Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 of 6 tasks
shivam-lal-maersk opened this issue Jun 10, 2024 · 2 comments · May be fixed by #18998
Closed
5 of 6 tasks

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

shivam-lal-maersk opened this issue Jun 10, 2024 · 2 comments · May be fixed by #18998

Comments

@shivam-lal-maersk
Copy link

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

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
@jpfinne
Copy link
Contributor

jpfinne commented Jun 16, 2024

@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 #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

@wing328
Copy link
Member

wing328 commented Jan 13, 2025

removeEnumValuePrefix is now default to false via #20452

@wing328 wing328 closed this as completed Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants