Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift3] primitive typed parameters in POST body #5500

Closed
TeMob opened this issue Apr 27, 2017 · 5 comments · Fixed by #6531
Closed

[Swift3] primitive typed parameters in POST body #5500

TeMob opened this issue Apr 27, 2017 · 5 comments · Fixed by #6531

Comments

@TeMob
Copy link

TeMob commented Apr 27, 2017

Description

Hi
I am struggling with primitive parameters used in the request body of POST requests.
The problem is that primitive parametertypes, such as "string", are forced to be JSON encoded, because all body-parameters are being JSON encoded.
Thats part of the problem. Another problem is that all parameters are being casted to a [String:AnyObject] dictionary, although a plain string will never succeed in doing so.

This is the code that swagger-codegen generated:

open class func myRequestWithRequestBuilder(xyz: String) -> RequestBuilder<ABC> {
	let path = ...
	let URLString = ...
	let parameters = xyz.encodeToJSON() as?  [String:AnyObject]  //<- this will always fail for primitives
	
	let url = NSURLComponents(string: URLString)
	
	let requestBuilder..
	return requestBuilder.init(method: "POST", URLString: (url?.string ?? URLString), parameters: parameters, isBody: true)
}
Swagger-codegen version

2.2.2

Swagger declaration file content or url
post:
   tags:
      - "MyApp"
   description: "description"
   consumes:
      - "text/plain"
   produces:
      - "application/json"
   parameters:
      - in: "body"
         name: "xyz"
         required: true
         schema:
         type: "string"
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i /path/to/my.yaml -l swift3 -o swaggeroutput/

@wing328
Copy link
Contributor

wing328 commented May 1, 2017

@TeMob can you give an example of the HTTP body? we want to know the request body looks like.

@TeMob
Copy link
Author

TeMob commented May 5, 2017

I can't get it to produce a request, so I can't show you the HTTP-request it sends.

@wing328
Copy link
Contributor

wing328 commented May 17, 2017

@TeMob I'm not referring to the HTTP request produced by the Swift3 API client.

I want to take a look at the correct HTTP request body to understand what payload is expected by the RESTful backend.

@jahertor
Copy link

@wing328 I have the same problem. The codegen are trying to parse every body parameter to JSON, even when in swagger editor I put String as input and consumes is text/plain. This is a problem for example when using JWT.

Expected request:

---> HTTP PUT http://localhost/api/users
Content-Type: text/plain; charset=UTF-8
Content-Length: 956
eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiUlNBLU9BRVAifQ...
---> END HTTP (956-byte body)

@wing328
Copy link
Contributor

wing328 commented Jul 26, 2017

@jahertor thanks for sharing more with an example. I think the issue is that Swift3 only supports JSON payload (body parameter) at the moment.

Would you have you time to contribute the enhancements? I can show you some good starting points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants