java-spring-cloud-stream-template icon indicating copy to clipboard operation
java-spring-cloud-stream-template copied to clipboard

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

Open pasinski opened this issue 2 years ago • 2 comments

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

pasinski avatar Nov 08 '22 07:11 pasinski

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[bot] avatar Nov 08 '22 07:11 github-actions[bot]

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

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 :heart:

github-actions[bot] avatar Mar 09 '23 00:03 github-actions[bot]