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

Missing individual parts content-types in multipart/formdata requests. #2954

Closed
mateusbandeiraa opened this issue May 13, 2023 · 6 comments
Closed

Comments

@mateusbandeiraa
Copy link

mateusbandeiraa commented May 13, 2023

Q&A (please complete the following information)

  • OS: MacOS
  • Environment: Chrome 115.0.5762.4, Node v18.16.0
  • Method of installation: npm
  • Swagger-Client version: 3.19.7
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Swagger/OpenAPI definition:

openapi: 3.0.0
info:
  title: 'Test Swagger'
  description: 'Test Swagger'
  contact:
    email: test@swagger.com
  version: '1'
servers:
  - url: https://demo7917836.mockable.io/
paths:
  /api/v1/profile:
    post:
      tags:
        - Profile
      summary: 'profile of the user user'
      requestBody:
        description: 'Profile that needs to be stored'
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                avatar_image:
                  description: avatar_image
                  type: string
                  format: binary
                profile:
                  $ref: '#/components/schemas/Profile'
            encoding:
              profile:
                contentType: application/json
      responses:
        201:
          description: 'Profile that was registered.'
          content:
            application/json:
              schema:
                type: object
        422:
          description: 'Unprocessable Entity.'
          content:
            application/json:
              schema:
                type: object
components:
  schemas:
    Profile:
      title: Profile
      description: 'profile model'
      required:
        - email
        - telephone
      properties:
        email:
          description: 'Email of the profile'
          type: string
        name:
          description: 'Name of the profile'
          type: string
      type: object
tags:
  -
    name: Profile
    description: 'Operations for the user profile'

Swagger-Client usage:

SwaggerClient({
  // your config options here
})

Describe the bug you're encountering

To reproduce...

Steps to reproduce the behavior:

  1. Execute the request POST /api/v1/profile with avatar_image as a png file, and profile as a JSON string

Expected behavior

The generated request body should be:

------WebKitFormBoundaryCqXhiFiwHDcc41AS
Content-Disposition: form-data; name="avatar_image"; filename="Captura de Tela 2023-05-13 às 14.57.47.png"
Content-Type: image/png


------WebKitFormBoundaryCqXhiFiwHDcc41AS
Content-Disposition: form-data; name="profile"
Content-Type: application/json

{
  "email": "string",
  "name": "string"
}
------WebKitFormBoundaryCqXhiFiwHDcc41AS--

(notice Content-Type: application/json in the profile part)

Actual behavior

The actual generated request body is:

------WebKitFormBoundary5Y9MMKP3nHioumLz
Content-Disposition: form-data; name="avatar_image"; filename="Captura de Tela 2023-05-13 às 14.57.47.png"
Content-Type: image/png


------WebKitFormBoundary5Y9MMKP3nHioumLz
Content-Disposition: form-data; name="profile"

{
  "email": "string",
  "name": "string"
}
------WebKitFormBoundary5Y9MMKP3nHioumLz--

(notice the missing Content-Type: application/json in the profile part)

Additional context or thoughts

This issue was initially created as swagger-api/swagger-ui#5356, but the required code change to fix this issue is in this repository.

@mateusbandeiraa
Copy link
Author

I created a draft PR addessing this issue. I could use some help, since this would be my first contribution. #2955

@mateusbandeiraa
Copy link
Author

@char0n Could you take a look at this, please? I have zero familiarity with the project but I really want to fix this bug. Can you give me some guidance on the draft PR?

@char0n
Copy link
Member

char0n commented May 29, 2023

Hi @mateusbandeiraa, all right, let me have a look. And thanks for contributing!

@char0n
Copy link
Member

char0n commented May 29, 2023

@mateusbandeiraa can you also please provide the actual code that reproduces this issue? I need an example which I can run and see the results you're describing.

@mateusbandeiraa
Copy link
Author

I was doing my testing using swagger ui, with the yaml provided in the issue description. I was able to see the generated requests with the browser DevTools.

openapi: 3.0.0
info:
  title: 'Test Swagger'
  description: 'Test Swagger'
  contact:
    email: test@swagger.com
  version: '1'
servers:
  - url: https://demo7917836.mockable.io/
paths:
  /api/v1/profile:
    post:
      tags:
        - Profile
      summary: 'profile of the user user'
      requestBody:
        description: 'Profile that needs to be stored'
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                avatar_image:
                  description: avatar_image
                  type: string
                  format: binary
                profile:
                  $ref: '#/components/schemas/Profile'
            encoding:
              profile:
                contentType: application/json
      responses:
        201:
          description: 'Profile that was registered.'
          content:
            application/json:
              schema:
                type: object
        422:
          description: 'Unprocessable Entity.'
          content:
            application/json:
              schema:
                type: object
components:
  schemas:
    Profile:
      title: Profile
      description: 'profile model'
      required:
        - email
        - telephone
      properties:
        email:
          description: 'Email of the profile'
          type: string
        name:
          description: 'Name of the profile'
          type: string
      type: object
tags:
  -
    name: Profile
    description: 'Operations for the user profile'

image
(the screenshot is from editor.swagger.io because I don't have my personal computer with me right now, but I was serving my own swagger instance for development.)

char0n added a commit that referenced this issue Aug 1, 2023
This change affects building requests from OpenAPI 3.x.y definitions.

Refs swagger-api/swagger-ui#5356
Refs swagger-api/swagger-ui#4826
Refs #2954
@char0n
Copy link
Member

char0n commented Aug 1, 2023

Addressed in #3078

@char0n char0n closed this as completed Aug 1, 2023
swagger-bot pushed a commit that referenced this issue Aug 1, 2023
# [3.20.0](v3.19.11...v3.20.0) (2023-08-01)

### Features

* **http:** respect Encoding Object while building requests ([#3078](#3078)) ([928cd40](928cd40)), closes [#2954](#2954)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants