0

I have an application with the following structure (simplified for brevity):

project structure

myApp
    src/main/java/
        com.something
            controllers
                UserApi
                UserController
            model
                UpsertRequest
    pom.xml
myApp-generated-client
    target
    pom.xml

UserApi

@PutMapping(USER_UPSERT_PATH)
    UpsertResponse userUpsert(
            @RequestBody UpsertRequest upsertRequest,
            @RequestHeader(name = COMPRESSED_DATA, required = false) byte[] compressedData);

pom.xml (myApp):

 <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>3.1.8</version>
                <configuration>
                    <skipSwaggerGeneration>false</skipSwaggerGeneration>
                    <apiSources>
                        <apiSource>
                            <schemes>http,https</schemes>
                            <swaggerDirectory>
                                ${project.basedir}/../myApp-generated-client/
                            </swaggerDirectory>
                            <locations>
                                <location>com.something.controllers</location>
                            </locations>
                            <springmvc>true</springmvc>
                            <outputFormats>json</outputFormats>
                            <attachSwaggerArtifact>true</attachSwaggerArtifact>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

pom.xml (myApp-generated-client)

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.1.2</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/swagger.json
                            </inputSpec>
                            <supportingFilesToGenerate>
                                ApiClient.java,JSON.java,OAuth.java,Authentication.java,ApiKeyAuth.java,HttpBasicAuth.java,ApiException.java,ApiResponse.java,Configuration.java,Pair.java,RFC3339DateFormat.java,StringUtil.java,CustomInstantDeserializer.java,Client.java
                            </supportingFilesToGenerate>
                            <skipValidateSpec>true</skipValidateSpec>
                            <apiPackage>com.something.api</apiPackage>
                            <generatorName>java</generatorName>
                            <templateDirectory>${project.basedir}/templates</templateDirectory>
                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>
                            <generateModels>true</generateModels>
                            <configOptions>
                                <library>jersey2</library>
                            </configOptions>
                            <typeMappings>Resource=File</typeMappings>
                            <importMappings>
                                <importMapping>UpsertRequest=com.something.model.upsertRequest</importMapping>
                            </importMappings>
                            <modelPackage>com.something.api</modelPackage>
                            <modelsToGenerate/>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

The problem is after compiling, my generated client keeps using List<byte[]> instead of byte[] for the upsert endpoint:

public ApiResponse<UpsertResponse> userUpsertWithHttpInfo(List<byte[]> compressedData, UpsertRequest body) throws ApiException

I read here that

Arrays become lists when you use the Swagger Codegen tool to generate Java clients. For example, a field of type String[] becomes List.

Is there any way of overcoming this limitation (i.e., some converter class or some annotation)? For that matter, is there any good explanations/documentation about how to use converters to resolve types?

Update:

After seeing this link, I added the following code and updated my generated-client pom, but am getting the same results:

package com.something.converter;

public class ByteArrayFixerModelConverter extends AbstractModelConverter {

    public ByteArrayFixerModelConverter() {
        super(Json.mapper());
    }

    @Override
    public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
        if (isByteArray(type)) {
            //bypass the chain! It would convert the ByteArrayProperty to an Array of ByteArrayProperty (bug in ModelModifier I think)
            return new ByteArraySchema();
        }
        return chain.hasNext()
                ? chain.next().resolve(type, context, chain)
                : null;
    }

    private boolean isByteArray(AnnotatedType annotatedType) {
        Type type = annotatedType.getType();
        boolean ret = type instanceof Class && type == byte[].class;
        if (!ret && type instanceof ArrayType) {
            ArrayType at = (ArrayType) type;
            JavaType contentType = at.getContentType();
            if (contentType instanceof SimpleType) {
                SimpleType st = (SimpleType) contentType;
                ret = st.getRawClass() == byte.class;
            }
        }
        return ret;
    }
}

pom.xml (myApp-generated-client, updated)

            <plugin>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <modelConverterClasses>com.something.converter.ByteArrayFixerModelConverter</modelConverterClasses>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
... //same as before
            </plugin>

Adam G
  • 39
  • 1
  • 2

0 Answers0