Skip to content

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

@davidmoten

Description

@davidmoten
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>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions