swagger-api / swagger-api/swagger-codegen-generators
Spring code generation: default response rendered with 200 error code
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:
- Go to 'Generate server >> spring'
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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