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

[REQ] [Swift5] Encode a user-defined model to Json when using multipart/form-data #8925

Closed
0x0c opened this issue Mar 8, 2021 · 4 comments · Fixed by #11202
Closed

[REQ] [Swift5] Encode a user-defined model to Json when using multipart/form-data #8925

0x0c opened this issue Mar 8, 2021 · 4 comments · Fixed by #11202

Comments

@0x0c
Copy link
Contributor

0x0c commented Mar 8, 2021

Is your feature request related to a problem? Please describe.

When sending any user-defined models as multipart/form-data, Xcode could not build generated codes because of missing a method called encodeToJSON that is a member of JSONEncodable protocol.
Although existing primitive types conforms to this protocol, user-defined model does not.

For example, Xcode could not build this API.

openapi: 3.0.0
info:
  title: test
  version: '1.0'
servers:
  - url: 'http://localhost:3000'
paths:
  /postModel:
    post:
      summary: Create New User
      operationId: post-user
      responses:
        '200':
          description: User Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
              examples: {}
        '400':
          description: Missing Required Information
      description: Create a new user.
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/Request'
    parameters: []
components:
  schemas:
    User:
      title: User
      type: object
      description: ''
      x-examples: {}
      properties:
        integerValue:
          type: integer
    Request:
      title: Request
      type: object
      properties:
        user1:
          $ref: '#/components/schemas/User'

Screen Shot 2021-03-09 at 1 14 33

Describe the solution you'd like

Implement encodeToJSON method for any user-defined models by protocol extension.

extension JSONEncodable where Self: Encodable {
    func encodeToJSON() -> Any {
        let encoder = JSONEncoder()
        guard let data = try? encoder.encode(self) else {
            fatalError("Could not encode to json: \(self)")
        }
        return data.base64EncodedString(options: Data.Base64EncodingOptions())
    }
}

Then, to confirm JSONEncodable for user-defined model, edit modelObject.mustache.

I already implement these codes and tested using 101da6e
If this request is acceptable, I will send PR.

@4brunu
Copy link
Contributor

4brunu commented Nov 30, 2021

Hey, could you please open a PR, so that we can try to fix this issue? Thanks 👍

@0x0c
Copy link
Contributor Author

0x0c commented Dec 30, 2021

@4brunu I open PR #11202. Would you review the PR?

@kimdv
Copy link

kimdv commented Mar 3, 2022

Hi!
With the latest version of the generator, 5.4.0 it add JSONEncodable to all our models.
Is there a way do disable adding JSONEncodable?

@4brunu
Copy link
Contributor

4brunu commented Mar 3, 2022

Currently no.
The 5.4.0 has a known issue that will be fixed in the next version.
So maybe you want to keep using 5.3.X while the next version is not released.
Otherwise you could create a jar by building this project from source.

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