swagger-api / swagger-api/swagger-codegen

[JAVA] selectHeaderAccept is removing values of the accept header

Open
#12,422 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated ApiClient.java method selectHeaderAccept and its isJsonMime check, then reproduce the behavior through swagger-codegen-maven-plugin using the provided configuration and Accept values. Trace the corresponding Java generation template and verify that generated clients retain all applicable Accept values instead of returning prematurely.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.