OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] spring server returns application/json String without quotes (invalid json)

Open
#15,463 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When using spring server generated by openapi-generator-maven-plugin (plugin xml further below), we have a method that returns a simple json string. The problem is that the String reponse of abc should be returned by the server as "abc" but is missing the quotes, thus is invalid json, and the client side fails to process it (especially as the returned Content-Type is application-json, just as declared in api yaml).

Here is a fragment from the generated interface:

    /**
     * GET /jsonString
     *
     * @return ok response (status code 200)
     */
    @Operation(
        operationId = "jsonStringGet",
        responses = {
            @ApiResponse(responseCode = "200", description = "ok response", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = String.class))
            })
        }
    )
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/jsonString",
        produces = { "application/json" }
    )
    default ResponseEntity<String> jsonStringGet(
        
    ) {
        return getDelegate().jsonStringGet();
    }

I assume the generated code could be improved for this case so that perhaps the method returned ResponseEntity<StringValue> where the property inside StringValue is annotated with @JsonValue.

openapi-generator version

6.5.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
    
  title: path tests
  version: v0.1

paths:
          
  /jsonString:
    get:
      responses:
        '200':    
          description: ok response
          content:
            application/json:
              schema: 
                $ref: '#/components/schemas/Name'
             
components:
  schemas:
    Name:
      type: string
Steps to reproduce

Maven plugin fragment that points at above yml:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/openapi/demo.yml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>demo.api</apiPackage>
                            <modelPackage>demo.model</modelPackage>
                            <invokerPackage>demo.client</invokerPackage>
                            <configOptions>
                                <delegatePattern>true</delegatePattern>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

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 by reproducing the generated Spring server with the OpenAPI YAML schema for the Name string and the shown openapi-generator-maven-plugin configuration. Inspect how the generated endpoint returns ResponseEntity and how the application/json response is serialized. Done means a response containing abc is emitted as valid JSON "abc" and remains processable by the client.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.