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

Duplicated values in enum throw IllegalStateException #6829

Open
heruan opened this issue Jan 26, 2025 · 5 comments
Open

Duplicated values in enum throw IllegalStateException #6829

heruan opened this issue Jan 26, 2025 · 5 comments

Comments

@heruan
Copy link

heruan commented Jan 26, 2025

Describe the bug

I'm generating class files with Fabric8 Gradle plugin (7.0.1) from the CloudNativePG CRDs (extra annotations enabled). When Sundrio's processes annotations, it fails with:

java.lang.IllegalStateException: Found multiple properties in: Action that map to: replace. Properties: Replace and replace!
        at io.sundr.builder.internal.checks.DublicatePropertyCheck.visit(DublicatePropertyCheck.java:28)

I assume this is related to this part of the CRD:

                      items:
                        description: |-
                          RelabelConfig allows dynamic rewriting of the label set for targets, alerts,
                          scraped samples and remote write samples.
                          
                          More info: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
                        properties:
                          action:
                            default: replace
                            description: |-
                              Action to perform based on the regex matching.
                              
                              `Uppercase` and `Lowercase` actions require Prometheus >= v2.36.0.
                              `DropEqual` and `KeepEqual` actions require Prometheus >= v2.41.0.
                              
                              Default: "Replace"
                            enum:
                              - replace
                              - Replace
                              - keep
                              - Keep
                              - drop
                              - Drop

But I cannot change the CRD nor the generated classes. I've tried setting uppercaseEnums both to true and false, same error.

Is it possible for the generator to skip duplicated enums?

Fabric8 Kubernetes Client version

SNAPSHOT

Steps to reproduce

Run the generator with extra annotations on a CRD that has enums like:

                            enum:
                              - replace
                              - Replace
                              - keep
                              - Keep
                              - drop
                              - Drop

The enum will be generated as

enum Action {
    REPLACE,
    _REPLACE,
    KEEP,
    _KEEP
    DROP,
    _DROP;
}

or, if enumUppercase=false, as

enum Action {
    replace,
    Replace,
    keep,
    Keep,
    drop,
    Drop;
}

In both cases, Sundrio will fail with:

java.lang.IllegalStateException: Found multiple properties in: Action that map to: replace.

Expected behavior

An enum without duplicated value, like:

enum Action {
    REPLACE,
    KEEP,
    DROP;
}

Runtime

Kubernetes (vanilla)

Kubernetes API Server version

1.25.3@latest

Environment

macOS

Fabric8 Kubernetes Client Logs

Additional context

No response

@heruan
Copy link
Author

heruan commented Jan 26, 2025

Looks like @JsonAlias can be used to deserialize multiple enum variations to a single enum value: https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-annotations/latest/index.html

For example:

enum Action {

    @JsonAlias({ "Replace", "replace" })
    REPLACE,

    @JsonAlias({ "Keep", "keep" })
    KEEP,

    @JsonAlias({ "Drop", "drop" })
    DROP;
}

It would be great to have the generator using this annotation instead of adding duplicates.

@shawkins
Copy link
Contributor

@manusa @andreaTP this was already fixed upstream with sundrio/sundrio#470

Changing the java generation as heruan suggests could be done as well, but without additional information I'm a little fuzzy on how the user would control which casing was ultimately used.

@heruan
Copy link
Author

heruan commented Jan 26, 2025

I'm a little confused: is a CRD enum with casing variations currently not supported? Seems common, at least it's used in widely used operators as in CloudNativePG. Am I missing some option to generate types compatible with Sundrio processor?

@shawkins
Copy link
Contributor

@heruan it's just a bug. There hasn't been a release of sundrio with the fix yet for fabric8 to pick up. cc @iocanel

@andreaTP
Copy link
Member

Hi @heruan , thanks for reporting this issue.

Let's cc. @manusa and we can self service a release of sundrio if needed.

A possible workaround to the problem would be to temporary override the class containing the repeated enum using existingJavaTypes in the java-generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants