OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.63k stars 6.29k forks source link

[BUG][JAVA] import mapping of alias types in API interface #8548

Open PhilippParis opened 3 years ago

PhilippParis commented 3 years ago

Bug Report Checklist

Description

when using importMapping for alias types, the generated code does not use the importmapping in the api interface

Expected
    @POST
    @Path("/{id}")
    Response typeAliasIdPost(@PathParam("id") foo.bar.TypeAlias id);
Actual
    @POST
    @Path("/{id}")
    Response typeAliasIdPost(@PathParam("id") String id);
openapi-generator version

5.0.0

OpenAPI declaration file content or url

https://gist.github.com/PhilippParis/28c40d590b4a9019cb8040b143e09884

Generation Details
<generatorName>jaxrs-spec</generatorName>
<interfaceOnly>true</interfaceOnly>
 <java8>true</java8>
<importMappings>
    <importMapping>TypeAlias=foo.bar.TypeAlias</importMapping>
</importMappings>
Steps to reproduce

run openapi-generator 5.0.0 with specified generation details

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/3589

borsch commented 3 years ago

The problem here is that you describe this object by your own here https://gist.github.com/PhilippParis/28c40d590b4a9019cb8040b143e09884#file-openapi-yaml-L36

to work with alias do the following

  1. don't create you own definition for alias in yaml file
  2. in place where you need to use that alias don't $ref them, but just use type: TypeAlias

At least this is how I configured alias for my project. Version of openapi-maven-plugin-generator is 4.3.1 Example:

/type-alias/{id}:
    post:
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: TypeAlias     <--
      responses:
        200:
          description: OK
PhilippParis commented 3 years ago

I've just tested this, and it also works in version 5.0.0 But without the definition of the alias type the yaml is an incomplete api specification and cannot be used by a third party..

borsch commented 3 years ago

I'll fix this

Zomzog commented 3 years ago

I think the issue is that "TypeAlias" is a String, It's impossible to generate in java such object that extends String. How looks the custom implementation provided?

creckord commented 3 years ago

The problem here is that in DefaultCodegen#fromParameter, the schema gets prematurely dereferenced - losing the schema name that is later used to handle the importMapping/typeMapping/languageSpecificPrimitives stuff through a call to DefaultCodegen#fromProperty.

In my projects, everything looks good when I either avoid the dereferencing in DefaultCodegen#fromParameter:

    parameterSchema = unaliasSchema(parameterSchema, importMapping); // <-- this was Collections.emptySet()

or hand the original schema of the parameter into DefaultCodegen#fromProperty:

   CodegenProperty codegenProperty = fromProperty(parameter.getName(), 
            parameter.getSchema() != null ? parameter.getSchema() : parameterSchema);

However, I have no clue why the unaliasSchema was done without importMappings in the first place, so I'm not sure what breakage this might cause (at least all tests stay green).