swagger-api / swagger-api/swagger-codegen-generators

Spring code generation: default response rendered with 200 error code

Open
#360 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

Hi guys,

I think there's something wrong in the way the default response in paths with spring code generator is handled.

Q&A
  • OS: macOS
  • Browser: chrome
  • Version: 72.0.3626.96
  • Method of installation: npm
  • Swagger-Editor version: 3.6.25
  • Swagger/OpenAPI version: OpenAPI 3.0
Content & configuration

Given this definition:

openapi: 3.0.0
info:
  title: title
  description: description
  version: '0.1'
paths:
  /get:
    summary: summary
    get:
      responses:
        '200':
          description: description
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/simple'
        default:
          description: error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/error'
components:
  schemas:
    simple:
      properties:
        myProp:
          type: string
    error:
      properties:
        errorMessage:
          type: string

and the sentence from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#responseObject:

A 200 response for a successful operation and a default response for others (implying an error):

Describe the bug you're encountering
To reproduce...

Steps to reproduce the behavior:

  1. Go to 'Generate server >> spring'
  2. Open the generated code
Expected behavior

I would expect the default answer in case to be mapped to an error code.

Actual behavior

Hereby the generated code:

package io.swagger.api;

import io.swagger.model.Error;
import io.swagger.model.Simple;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2019-03-20T11:00:13.697Z[GMT]")
@Api(value = "get", description = "the get API")
public interface GetApi {

    @ApiOperation(value = "", nickname = "getGet", notes = "", response = Simple.class, tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "description", response = Simple.class),
        @ApiResponse(code = 200, message = "error", response = Error.class) })
    @RequestMapping(value = "/get",
        produces = { "application/json" }, 
        method = RequestMethod.GET)
    ResponseEntity<Simple> getGet();

}

As you can see, error is mapped with a 200 error code

Additional context or thoughts

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 Java GetApi example in the issue and trace the Spring generator or Mustache template that produces its @ApiResponse entries. Reproduce the OpenAPI definition with Generate server >> spring, then verify that the default response is emitted with an appropriate non-200 error code and that the generated response type remains correct.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.