-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[kotlin][jvm-okhttp4] Fix multipart/form-data with JSON content-type #22856
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
Merged
wing328
merged 6 commits into
OpenAPITools:master
from
alexhumphreys:fix/kotlin-multipart-json-content-type
Feb 4, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50a7e12
[kotlin][jvm-okhttp4] Fix multipart/form-data with JSON content-type
alexhumphreys 4e1607e
Run mvn clean/package, and regenerate samples
alexhumphreys 298e7bb
Add fix for kotlinx serialisation issue
alexhumphreys af29043
Refactor multipart helpers for reified type parameter support
alexhumphreys c58de05
Fix kotlinx.serialization multipart by adding serializer lambda to Pa…
alexhumphreys b496b4d
Fix internal Ktor API usage in multipart forms
alexhumphreys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,10 @@ import io.ktor.client.request.forms.formData | |
| import io.ktor.client.engine.HttpClientEngine | ||
| import kotlinx.serialization.json.Json | ||
| import io.ktor.http.ParametersBuilder | ||
| import io.ktor.http.Headers | ||
| import io.ktor.http.HttpHeaders | ||
| import io.ktor.http.ContentType | ||
| import io.ktor.http.content.PartData | ||
| import kotlinx.serialization.* | ||
| import kotlinx.serialization.descriptors.* | ||
| import kotlinx.serialization.encoding.* | ||
|
|
@@ -76,11 +80,31 @@ import kotlinx.serialization.encoding.* | |
| {{#formParams}} | ||
| {{#isArray}} | ||
| {{{paramName}}}?.onEach { | ||
| {{#isFile}}append(it){{/isFile}}{{^isFile}}append("{{{baseName}}}", it){{/isFile}} | ||
| {{#isFile}}append(it){{/isFile}}{{^isFile}}append("{{{baseName}}}", it.toString()){{/isFile}} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Multipart arrays of non-file objects are still serialized with Prompt for AI agents |
||
| } | ||
| {{/isArray}} | ||
| {{^isArray}} | ||
| {{{paramName}}}?.apply { {{#isFile}}append({{{baseName}}}){{/isFile}}{{^isFile}}append("{{{baseName}}}", {{^isEnumOrRef}}{{{paramName}}}{{/isEnumOrRef}}{{#isEnumOrRef}}{{{paramName}}}.value{{/isEnumOrRef}}){{/isFile}} } | ||
| {{#isFile}} | ||
| {{{paramName}}}?.apply { append({{{baseName}}}) } | ||
| {{/isFile}} | ||
| {{^isFile}} | ||
| {{#isPrimitiveType}} | ||
| {{#isString}} | ||
| {{{paramName}}}?.apply { append("{{{baseName}}}", {{{paramName}}}) } | ||
| {{/isString}} | ||
| {{^isString}} | ||
| {{{paramName}}}?.apply { append("{{{baseName}}}", {{{paramName}}}.toString()) } | ||
| {{/isString}} | ||
| {{/isPrimitiveType}} | ||
| {{^isPrimitiveType}} | ||
| {{#isEnumOrRef}} | ||
| {{{paramName}}}?.apply { append("{{{baseName}}}", {{{paramName}}}.value.toString()) } | ||
| {{/isEnumOrRef}} | ||
| {{^isEnumOrRef}} | ||
| {{{paramName}}}?.apply { append("{{{baseName}}}", ApiClient.JSON_DEFAULT.encodeToString({{{dataType}}}.serializer(), {{{paramName}}})) } | ||
| {{/isEnumOrRef}} | ||
| {{/isPrimitiveType}} | ||
| {{/isFile}} | ||
| {{/isArray}} | ||
| {{/formParams}} | ||
| } | ||
|
|
||
69 changes: 69 additions & 0 deletions
69
modules/openapi-generator/src/test/resources/3_0/kotlin/echo_multipart_json.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| openapi: 3.0.0 | ||
| servers: | ||
| - url: 'http://localhost:3000/' | ||
| info: | ||
| version: 1.0.0 | ||
| title: Echo API for Kotlin Multipart JSON Test | ||
| description: Echo server API to test multipart/form-data with JSON content-type | ||
| license: | ||
| name: Apache-2.0 | ||
| url: 'https://www.apache.org/licenses/LICENSE-2.0.html' | ||
| tags: | ||
| - name: body | ||
| description: Test body operations | ||
| paths: | ||
| /body/multipart/formdata/with_json_part: | ||
| post: | ||
| tags: | ||
| - body | ||
| summary: Test multipart with JSON part | ||
| description: Test multipart/form-data with a part that has Content-Type application/json | ||
| operationId: testBodyMultipartFormdataWithJsonPart | ||
| requestBody: | ||
| required: true | ||
| content: | ||
| multipart/form-data: | ||
| schema: | ||
| type: object | ||
| required: | ||
| - metadata | ||
| - file | ||
| properties: | ||
| metadata: | ||
| $ref: '#/components/schemas/FileMetadata' | ||
| file: | ||
| type: string | ||
| format: binary | ||
| description: File to upload | ||
| encoding: | ||
| metadata: | ||
| contentType: application/json | ||
| file: | ||
| contentType: image/jpeg | ||
| responses: | ||
| '200': | ||
| description: Successful operation | ||
| content: | ||
| text/plain: | ||
| schema: | ||
| type: string | ||
| components: | ||
| schemas: | ||
| FileMetadata: | ||
| type: object | ||
| required: | ||
| - id | ||
| - name | ||
| properties: | ||
| id: | ||
| type: integer | ||
| format: int64 | ||
| example: 12345 | ||
| name: | ||
| type: string | ||
| example: test-file | ||
| tags: | ||
| type: array | ||
| items: | ||
| type: string | ||
| example: ["tag1", "tag2"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
samples/client/echo_api/kotlin-jvm-okhttp-multipart-json/.openapi-generator-ignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # OpenAPI Generator Ignore | ||
| # Generated by openapi-generator https://github.com/openapitools/openapi-generator | ||
|
|
||
| # Use this file to prevent files from being overwritten by the generator. | ||
| # The patterns follow closely to .gitignore or .dockerignore. | ||
|
|
||
| # As an example, the C# client generator defines ApiClient.cs. | ||
| # You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: | ||
| #ApiClient.cs | ||
|
|
||
| # You can match any string of characters against a directory, file or extension with a single asterisk (*): | ||
| #foo/*/qux | ||
| # The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux | ||
|
|
||
| # You can recursively match patterns against a directory, file or extension with a double asterisk (**): | ||
| #foo/**/qux | ||
| # This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux | ||
|
|
||
| # You can also negate patterns with an exclamation (!). | ||
| # For example, you can ignore all files in a docs folder with the file extension .md: | ||
| #docs/*.md | ||
| # Then explicitly reverse the ignore rule for a single file: | ||
| #!docs/README.md |
24 changes: 24 additions & 0 deletions
24
samples/client/echo_api/kotlin-jvm-okhttp-multipart-json/.openapi-generator/FILES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| README.md | ||
| build.gradle | ||
| docs/BodyApi.md | ||
| docs/FileMetadata.md | ||
| gradle/wrapper/gradle-wrapper.jar | ||
| gradle/wrapper/gradle-wrapper.properties | ||
| gradlew | ||
| gradlew.bat | ||
| settings.gradle | ||
| src/main/kotlin/org/openapitools/client/apis/BodyApi.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/ApiAbstractions.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/ApiResponse.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/PartConfig.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/ResponseExtensions.kt | ||
| src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt | ||
| src/main/kotlin/org/openapitools/client/models/FileMetadata.kt |
1 change: 1 addition & 0 deletions
1
samples/client/echo_api/kotlin-jvm-okhttp-multipart-json/.openapi-generator/VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 7.20.0-SNAPSHOT |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Serializer lambda casts multipart part bodies to the declared data type even when the body is a String enum value or null, which will crash at runtime for enum and optional parameters.
Prompt for AI agents