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

Poem/Openapi : Strategy for generating generic structure names #774

Open
rxdiscovery opened this issue Mar 17, 2024 · 4 comments
Open

Poem/Openapi : Strategy for generating generic structure names #774

rxdiscovery opened this issue Mar 17, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@rxdiscovery
Copy link

rxdiscovery commented Mar 17, 2024

Hello,
Poem #[aoi] , must allow you to choose the strategy for generating generic structure names,

Response<T> for example, should have the symbol name Reponse_T or ResponseForT or ResponseOfT
and Response<[T]> => ResponseOfListT for example.

when I want to generate code with the swagger spec using openapi generator, I get this error because the generic names contain special characters :

Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 4, Warning count: 0
Errors: 
        -components.schemas.Schema name Response<string> doesn't adhere to regular expression ^[a-zA-Z0-9\.\-_]+$
        -attribute info.summary is unexpected
        -components.schemas.Schema name Response<Auth> doesn't adhere to regular expression ^[a-zA-Z0-9\.\-_]+$
        -components.schemas.Schema name Response<[City]> doesn't adhere to regular expression ^[a-zA-Z0-9\.\-_]+$

Response and Response<[City]> will generate the same structure : ResponseCity , ResponseCity, the generated code has no way of differentiating between them.

but if we rename the [City] to ListCity, for example, the result will be : ResponseCity and ResponseListCity

to simplify, here is the part concerned :

impl<T: Type> Type for Vec<T> {
    const IS_REQUIRED: bool = true;

    type RawValueType = Self;

    type RawElementValueType = T::RawValueType;

    fn name() -> Cow<'static, str> {
        format!("[{}]", T::name()).into()  //<<------------------------------------- !!!
    }

    fn schema_ref() -> MetaSchemaRef {
        MetaSchemaRef::Inline(Box::new(MetaSchema {
            items: Some(Box::new(T::schema_ref())),
            ..MetaSchema::new("array")
        }))
    }

transform [T] into ListOfT , because [ and ] characters are not allowed

    fn name() -> Cow<'static, str> {
        format!("[{}]", T::name()).into()  //<<------------------------------------- !!!
    }

to

    fn name() -> Cow<'static, str> {
        format!("ListOf{}", T::name()).into()  //<<------------------------------------- !!!
    }

Thanks in advance 🥇

@rxdiscovery rxdiscovery added the bug Something isn't working label Mar 17, 2024
@sunli829
Copy link
Collaborator

Currently it is not possible to customize the OpenAPI name corresponding to a generic type. If you need a legal name, you can only avoid using generics.

@rxdiscovery
Copy link
Author

I have a proposition,
why not take the template ( [{}] ) directly from a global variable, which can be modified?

  fn name() -> Cow<'static, str> {
        format!("[{}]", T::name()).into() 
    }

to

  fn name() -> Cow<'static, str> {
        format!(get_vec_gen_template(), T::name()).into() 
    }

@amtelekom
Copy link
Contributor

#318 and #670 are about similar problems.
#671 implemented a solution for the generics problem, but sadly got no feedback so far.

The current output is non-compliant with the OpenAPI spec, even if SwaggerUI & Co don't complain:

All the fixed fields declared above are objects that MUST use keys that match the regular expression: ^[a-zA-Z0-9.-_]+$

(https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#components-object)

The problem I see with both your suggestion (Prefix with ListOf instead of wrapping in brackets) as well as the one in #671 (replace special characters with underscore) is that it might collide with non-generics that use the same name - but IMHO both solutions would be preferable to just not be able to use Generics at all without generating invalid OpenAPI specs.

@sunli829 would you be open to a PR that removes the disallowed characters, and if so, do you have a preference for the format (i.e., replace punctuation with underscores, Prefix with a String like "Of", ...)?

@rxdiscovery
Copy link
Author

Hello,

@amtelekom if we make the template dynamic so that each developer can customize it only for their needs

I think @sunli829 is so busy lately, it would be nice if he assigned a moderator to help him.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants