sngular / scs-multiapi-plugin

This is a Maven plugin designed to help developers automatizing the creation of code classes from YML files based on AsyncApi and OpenAPI.
http://sngular.com
Mozilla Public License 2.0
48 stars 10 forks source link

Bug: No support for special char in emum #311

Closed bibiboss closed 10 months ago

bibiboss commented 10 months ago

Hello! First, I'm not sure to write correctly the issue, so I appologize for that. I'll try to be reactive if any information is missing.

It seems that the plugin does not support special char. So far I've been using : and .. Both this char can be used in a urn definition (https://www.rfc-editor.org/rfc/rfc2141). While some of my messages exchanged through a bus have URNs inside, it seems that currently it is not possible.

asyncapi.yml example

asyncapi: 2.5.0
info:
  title: Some test
  version: '1.0.0'
  description: |
    This document describes a test".

defaultContentType: application/json
channels:
  events/public/example/{testId}:
    description: The topic where tenant modification are published
    parameters:
      testId:
        $ref: '#/components/parameters/testId'
    subscribe:
      operationId: updateTest
      traits:
        - bindings:
            mqtt:
              retain: true
      message:
        $ref: '#/components/messages/test'

components:
  messages:
    test:
      name: test
      title: Testing the test
      summary: Same
      contentType: application/json
      payload:
        $ref: "#/components/schemas/test"

  schemas:
    test:
      type: object
      properties:
        source:
          type: string
          enum:
            - "urn:namespace:valid:namespace.specific"
        data:
          type: object
          properties:
            meaningfulData:
              type: string
              examples:
                - data
            someOtherData:
              type: string
              examples:
                - bob
    uuid:
      type: string
      description: A UUIDv6
      examples:
        - 69ce3aa7-437e-627c-8845-bd5765b15c6b
        - 1c1de518-507c-63d9-93b3-9e6b0e79c96d
        - e383b6f0-f685-6d60-8a89-2c09bc90c138
        - 0250050f-0b2a-6ff5-a1fd-1479e431bbec
        - bd89a2a2-ddf5-65c8-abd5-8165e41f9e12
      pattern: ^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$

  parameters:
    testId:
      description: The identifier of a test
      schema:
        type: string
        examples:
          - "toto"
          - "tata"

mvn configuration

          <plugin>
                <groupId>com.sngular</groupId>
                <artifactId>scs-multiapi-maven-plugin</artifactId>
                <version>5.1.0</version>
                <executions>
                    <execution>
                        <id>asyncapi</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>asyncapi-generation</goal>
                        </goals>
                        <configuration>
                            <specFiles>
                                <specFile>
                                    <filePath>${project.basedir}/test/asyncapi.yml</filePath>
                                    <supplier>
                                        <ids>updateTest</ids>
                                        <apiPackage>test.api</apiPackage>
                                        <modelPackage>test.api.model</modelPackage>
                                    </supplier>
                                </specFile>
                            </specFiles>
                            <generatedSourcesFolder>generated-sources</generatedSourcesFolder>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Result: Failing compilation image

cause: image

Possible result: The piece of code could be generated like:

@JsonDeserialize(builder = Test.TestBuilder.class)
public class Test {

  @JsonProperty(value ="source")
  private Source source;
  public enum Source {
    URN_NAMESPACE_VALID_NAMESPACE_SPECIFIC("urn:namespace:valid:namespace.specific");
 }
 // more stuff
}

Best regards

github-actions[bot] commented 10 months ago

Thank you for collaborating with the project by giving us feedback! Cheers!

bibiboss commented 10 months ago

Hello @jemacineiras I saw the tags you put, I use maven. I don't know if that's relevant for the issue.

jemacineiras commented 10 months ago

Hi @bibiboss, nowadays when we build an Enum type we are using the literals you add in the enum part as Identifiers. So as standard java language some characters are not allow, like ´:´

what will be produced right now is something like

public enum Source {

  urn:namespace:valid:namespace.specific

}

The solution here to support that kind of is build something like

public enum Source {

  URN_NAMESPACE_VALID_NAMESPACE_SPECIFIC("urn:namespace:valid:namespace.specific")

  private final String value;

  Source(final String value) {
    this.value = value;
  }

  public String getValue() {
    return value;
  }
}

That will work for you?

Cheers

bibiboss commented 10 months ago

Hello @jemacineiras

Yes :)

I think there is a list of char to check. On the top of my head I'm thinking about all specials char from regional keyboard (é and è for example), and other java-reserved chars.

Cheers

jemacineiras commented 10 months ago

Hi @bibiboss ,

there is a fix in the branch attached to this issue. If you can test it and works for you, then I can merge it.

Cheers

bibiboss commented 10 months ago

Hello @jemacineiras

It works well! Outside specials char like é or ñ that are transformed to _, but since they might be refused in many protocols, I guess we can state that is more related to a non-blocking bug (that is basically aestetics 😄 ).

I'm not stuck on this anymore!

Thanks again for quick answer and fix!