swagger-api / swagger-api/swagger-codegen

[SWIFT4] Response decoder doesn't work with primitives types

Open
#8,953 0 comments 0 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

Swagger-codegen version

2.4.0-SNAPSHOT

Description

When decoding the response in Swift4, it's work perfectly with JSON object, String, File (data) but it does not work with primitives type like boolean (just work with String)

To decode the response, a builder is used in "AlamofireDecodableRequestBuilder" class. This builder is AlamofireRequestBuilder where "T" is the type of response object.
In this class, there is a switch/case with the following types : String/Void/Data/default.
When the response type is Boolean or Integer for exemple, it runs in the default case.
But in this case, we are waiting for an object in JSON format. (see line CodableHelper.decode).
An error appears because decoder is waiting for JSON with {} tag.

In my rest API I have this for exemple :

@PUT
@SecuredToken
@Path("/test")
@ApiOperation(value = "test", response = Boolean.class)
@ApiResponses(value = {@ApiResponse(code = 200, message = "", response = Boolean.class)})
public Response test() {
	return Response.ok().entity(true).build();
}
"/test" : {
      "put" : {
        "tags" : [ "XXX" ],
        "summary" : "test",
        "description" : "",
        "operationId" : "test",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ], //i have test with text plain too
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "",
            "schema" : {
              "type" : "boolean"
            }
          }
        }
      }
    }
Suggest a fix/enhancement

In switch default case, see if the type is primitive, and only cast in this case.

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 at the Swift4 AlamofireDecodableRequestBuilder entry point and inspect how CodableHelper.decode handles response types. Reproduce the issue with Boolean and Integer responses, then verify that primitive JSON responses decode successfully alongside the existing object, String, Void, and Data cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.