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

Enums having uri-reference format won't compile in Java #303

Open
pasinski opened this issue Nov 8, 2022 · 2 comments
Open

Enums having uri-reference format won't compile in Java #303

pasinski opened this issue Nov 8, 2022 · 2 comments
Labels

Comments

@pasinski
Copy link

pasinski commented Nov 8, 2022

I created a simple api spec, where one field is enum and has a uri-reference format.

source:
            type: string
            format: uri-reference
            minLength: 1
            enum:
              - com.example

Such field will get generated in Java as public static enum Source { com.example }. Since com.example is not a valid enum identifier in Java, the code will not compile.

In this case, a valid approach would be to generate a class as follows (see also enum generation in https://github.com/asyncapi/java-spring-template):

public enum Source {
            
        COM_EXAMPLE(String.valueOf("com.example"));
            
        private String value;

        Source (String v) {
            value = v;
        }

        public String value() {
            return value;
        }

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

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

of course, @JsonValue can also be placed on value() method

@github-actions
Copy link

github-actions bot commented Nov 8, 2022

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@github-actions
Copy link

github-actions bot commented Mar 9, 2023

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant