swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.91k stars 6.03k forks source link

[JAVA] selectHeaderAccept is removing values of the accept header #12422

Open msnteixeira opened 2 months ago

msnteixeira commented 2 months ago
Description

T.L.D.R.: The selectHeaderAccept is removing values of the accept header

When working in JAVA and generating code using swagger-codegen-maven-plugin, the code generated contains in ApiClient.java the method public List<MediaType> selectHeaderAccept(String[] accepts):

public List<MediaType> selectHeaderAccept(String[] accepts) {
        if (accepts.length == 0) {
            return null;
        }
        for (String accept : accepts) {
            MediaType mediaType = MediaType.parseMediaType(accept);
            if (isJsonMime(mediaType)) {
                return Collections.singletonList(mediaType);
            }
        }
        return MediaType.parseMediaTypes(StringUtils.arrayToCommaDelimitedString(accepts));
    }

This method iterates over the values of accept header and returns early if JSON exists in the array. This verification is done using the public boolean isJsonMime(MediaType mediaType):

public boolean isJsonMime(MediaType mediaType) {
        return mediaType != null && (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType) || mediaType.getSubtype().matches("^.*\\+json[;]?\\s*$"));
    }

If the Accept header contains application/problem+json and application/json, and the client expects application/json, an error will occur since the selectHeaderAccept method returns early with a single value (effectively removing other possible JSON values) when it shouldn't.

Swagger-codegen version

latest, 3.0.58

Command line used for generation

Generated using swagger-codegen-maven-plugin

Steps to reproduce
<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.58</version>
    <executions>
        <execution>
            <id>ExampleClient</id>
            <phase>process-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.build.directory}/${swagger.rest.client.example}</inputSpec>
                <language>java</language>
                <library>resttemplate</library>
                <modelPackage>com.kn.example.client.model</modelPackage>
                <apiPackage>com.kn.example.client.api</apiPackage>
                <generateModelTests>false</generateModelTests>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateApiTests>false</generateApiTests>
                <generateApiDocumentation>false</generateApiDocumentation>
                <configOptions>
                    <hideGenerationTimestamp>true</hideGenerationTimestamp>
                    <dateLibrary>java8-localdatetime</dateLibrary>
                    <interfaceOnly>true</interfaceOnly>
                    <defaultInterfaces>false</defaultInterfaces>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
Suggest a fix/enhancement

Consider avoiding the premature optimization of returning the first isJsonMime found in the given Accept header array.

drobus commented 1 month ago

I have the same issue, however, I use https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-gradle-plugin/README.adoc plugin, but it is possible that it uses swagger-codegen underneath.