Skip to content

Commit c0701c5

Browse files
HavenDVclaude
andcommitted
fix: multipart object parts default to application/json
OpenAPI's default content type for a multipart part whose schema is an object is application/json. Without an encoding entry, a free-form object part (no declared properties, e.g. additionalProperties) had no content type and was sent as its ToString(); it is now labelled JSON and serialized as JSON. Objects with declared properties already were. Snapshot changes: photoroom 'layers', and opik 'file', which its spec declares as type: object. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b2f837a commit c0701c5

7 files changed

Lines changed: 111 additions & 16 deletions

File tree

‎src/libs/AutoSDK.CSharp/Operations/CSharpEndPointFactory.cs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ internal static EndPoint CreateEndPointWithCache(
189189
Summary = requestProperty.Summary,
190190
ConverterType = requestProperty.ConverterType,
191191
Description = requestProperty.Description,
192-
ContentType = encoding?.ContentType,
192+
ContentType = encoding?.ContentType ??
193+
GetDefaultMultipartPartContentType(requestMediaType, requestRepresentation.MediaTypeData, requestProperty.Id),
193194
Style = encoding?.Style,
194195
Explode = encoding?.Explode ?? false,
195196
UnionVariantNames = unionVariantNames,
@@ -504,6 +505,29 @@ private static void DeduplicateMethodParameterNames(List<MethodParameter> parame
504505
}
505506
}
506507

508+
/// <summary>
509+
/// OpenAPI's default content type for a multipart part whose schema is an object, when the request's
510+
/// <c>encoding</c> names none: <c>application/json</c>. Without it a free-form object (no declared
511+
/// properties) was sent as <c>ToString()</c> with no content type.
512+
/// </summary>
513+
private static string? GetDefaultMultipartPartContentType(
514+
string mediaType,
515+
IOpenApiMediaType? mediaTypeData,
516+
string propertyId)
517+
{
518+
if (!mediaType.NormalizeMimeType().Equals("multipart/form-data", StringComparison.OrdinalIgnoreCase) ||
519+
mediaTypeData?.Schema?.Properties is not { } properties ||
520+
!properties.TryGetValue(propertyId, out var property) ||
521+
property.Type is not { } type)
522+
{
523+
return null;
524+
}
525+
526+
return (type & ~JsonSchemaType.Null) == JsonSchemaType.Object
527+
? "application/json"
528+
: null;
529+
}
530+
507531
private static RequestPropertySelection GetRequestProperties(SchemaContext? requestContext)
508532
{
509533
if (requestContext == null)

‎src/tests/AutoSDK.SnapshotTests/Snapshots/CLI/opik/_#G.DatasetsClient.CreateDatasetItemsFromCsv.g.verified.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ await CreateDatasetItemsFromCsvAsResponseAsync(
109109
#endif
110110

111111
var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
112+
var __contentFile = new global::System.Net.Http.StringContent(global::System.Text.Json.JsonSerializer.Serialize((request.File!), JsonSerializerOptions));
113+
__contentFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
112114
__httpRequestContent.Add(
113-
content: new global::System.Net.Http.StringContent(request.File.ToString() ?? string.Empty),
115+
content: __contentFile,
114116
name: "\"file\"");
115117

116118
__httpRequestContent.Add(

‎src/tests/AutoSDK.SnapshotTests/Snapshots/opik/NewtonsoftJson/_#G.DatasetsClient.CreateDatasetItemsFromCsv.g.verified.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ await CreateDatasetItemsFromCsvAsResponseAsync(
109109
#endif
110110

111111
var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
112+
var __contentFile = new global::System.Net.Http.StringContent(global::Newtonsoft.Json.JsonConvert.SerializeObject((request.File!), JsonSerializerOptions));
113+
__contentFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
112114
__httpRequestContent.Add(
113-
content: new global::System.Net.Http.StringContent(request.File.ToString() ?? string.Empty),
115+
content: __contentFile,
114116
name: "\"file\"");
115117

116118
__httpRequestContent.Add(

‎src/tests/AutoSDK.SnapshotTests/Snapshots/opik/SystemTextJson/_#G.DatasetsClient.CreateDatasetItemsFromCsv.g.verified.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ await CreateDatasetItemsFromCsvAsResponseAsync(
109109
#endif
110110

111111
var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
112+
var __contentFile = new global::System.Net.Http.StringContent(global::System.Text.Json.JsonSerializer.Serialize((request.File!), JsonSerializerOptions));
113+
__contentFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
112114
__httpRequestContent.Add(
113-
content: new global::System.Net.Http.StringContent(request.File.ToString() ?? string.Empty),
115+
content: __contentFile,
114116
name: "\"file\"");
115117

116118
__httpRequestContent.Add(

‎src/tests/AutoSDK.SnapshotTests/Snapshots/photoroom/NewtonsoftJson/_#G.Api.EditImagePost.g.verified.cs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,13 @@ request.ImageFilename is null
601601
if (request.Layers != default)
602602
{
603603

604+
var __contentLayers = new global::System.Net.Http.StringContent(global::Newtonsoft.Json.JsonConvert.SerializeObject((request.Layers!), JsonSerializerOptions));
605+
__contentLayers.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
604606
__httpRequestContent.Add(
605-
content: new global::System.Net.Http.StringContent(request.Layers.ToString() ?? string.Empty),
607+
content: __contentLayers,
606608
name: "\"layers\"");
607-
608609
}
610+
609611
if (request.LightingMode != default)
610612
{
611613

@@ -1779,11 +1781,13 @@ request.ImageFilename is null
17791781
if (request.Layers != default)
17801782
{
17811783

1784+
var __contentLayers = new global::System.Net.Http.StringContent(global::Newtonsoft.Json.JsonConvert.SerializeObject((request.Layers!), JsonSerializerOptions));
1785+
__contentLayers.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
17821786
__httpRequestContent.Add(
1783-
content: new global::System.Net.Http.StringContent(request.Layers.ToString() ?? string.Empty),
1787+
content: __contentLayers,
17841788
name: "\"layers\"");
1785-
17861789
}
1790+
17871791
if (request.LightingMode != default)
17881792
{
17891793

@@ -2960,11 +2964,13 @@ request.ImageFilename is null
29602964
if (request.Layers != default)
29612965
{
29622966

2967+
var __contentLayers = new global::System.Net.Http.StringContent(global::Newtonsoft.Json.JsonConvert.SerializeObject((request.Layers!), JsonSerializerOptions));
2968+
__contentLayers.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
29632969
__httpRequestContent.Add(
2964-
content: new global::System.Net.Http.StringContent(request.Layers.ToString() ?? string.Empty),
2970+
content: __contentLayers,
29652971
name: "\"layers\"");
2966-
29672972
}
2973+
29682974
if (request.LightingMode != default)
29692975
{
29702976

‎src/tests/AutoSDK.SnapshotTests/Snapshots/photoroom/SystemTextJson/_#G.Api.EditImagePost.g.verified.cs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,13 @@ request.ImageFilename is null
601601
if (request.Layers != default)
602602
{
603603

604+
var __contentLayers = new global::System.Net.Http.StringContent(global::System.Text.Json.JsonSerializer.Serialize((request.Layers!), JsonSerializerOptions));
605+
__contentLayers.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
604606
__httpRequestContent.Add(
605-
content: new global::System.Net.Http.StringContent(request.Layers.ToString() ?? string.Empty),
607+
content: __contentLayers,
606608
name: "\"layers\"");
607-
608609
}
610+
609611
if (request.LightingMode != default)
610612
{
611613

@@ -1779,11 +1781,13 @@ request.ImageFilename is null
17791781
if (request.Layers != default)
17801782
{
17811783

1784+
var __contentLayers = new global::System.Net.Http.StringContent(global::System.Text.Json.JsonSerializer.Serialize((request.Layers!), JsonSerializerOptions));
1785+
__contentLayers.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
17821786
__httpRequestContent.Add(
1783-
content: new global::System.Net.Http.StringContent(request.Layers.ToString() ?? string.Empty),
1787+
content: __contentLayers,
17841788
name: "\"layers\"");
1785-
17861789
}
1790+
17871791
if (request.LightingMode != default)
17881792
{
17891793

@@ -2960,11 +2964,13 @@ request.ImageFilename is null
29602964
if (request.Layers != default)
29612965
{
29622966

2967+
var __contentLayers = new global::System.Net.Http.StringContent(global::System.Text.Json.JsonSerializer.Serialize((request.Layers!), JsonSerializerOptions));
2968+
__contentLayers.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
29632969
__httpRequestContent.Add(
2964-
content: new global::System.Net.Http.StringContent(request.Layers.ToString() ?? string.Empty),
2970+
content: __contentLayers,
29652971
name: "\"layers\"");
2966-
29672972
}
2973+
29682974
if (request.LightingMode != default)
29692975
{
29702976

‎src/tests/AutoSDK.UnitTests/MultipartRequestGenerationTests.cs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,57 @@ public void FreeFormJsonParts_AreSerializedAsJsonNotToString()
520520
methodCode.Should().NotContain("(request.Labels!).ToString()");
521521
methodCode.Should().Contain("var __contentNote = new global::System.Net.Http.StringContent(request.Note ?? string.Empty);");
522522
}
523+
524+
[TestMethod]
525+
public void ObjectPartsWithoutEncoding_DefaultToJson()
526+
{
527+
const string yaml = """
528+
openapi: 3.0.3
529+
info:
530+
title: Object parts without encoding
531+
version: 1.0.0
532+
paths:
533+
/upload:
534+
post:
535+
operationId: uploadFile
536+
requestBody:
537+
required: true
538+
content:
539+
multipart/form-data:
540+
schema:
541+
type: object
542+
required: [file]
543+
properties:
544+
metadata:
545+
type: object
546+
additionalProperties: true
547+
labels:
548+
type: object
549+
additionalProperties:
550+
type: string
551+
note:
552+
type: string
553+
count:
554+
type: integer
555+
file:
556+
type: string
557+
format: binary
558+
responses:
559+
'204':
560+
description: ok
561+
""";
562+
563+
var data = AutoSDK.Generation.Data.Prepare(((yaml, DefaultSettings), GlobalSettings: DefaultSettings));
564+
var method = data.Methods.Single(x => x.NotAsyncMethodName == "UploadFile");
565+
var methodCode = Sources.GenerateEndPoint(method);
566+
567+
method.Parameters.Single(x => x.Id == "metadata").ContentType.Should().Be("application/json");
568+
method.Parameters.Single(x => x.Id == "labels").ContentType.Should().Be("application/json");
569+
method.Parameters.Single(x => x.Id == "note").ContentType.Should().BeNull();
570+
method.Parameters.Single(x => x.Id == "count").ContentType.Should().BeNull();
571+
methodCode.Should().Contain("__contentMetadata.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue(\"application/json\")");
572+
methodCode.Should().Contain("global::System.Text.Json.JsonSerializer.Serialize((request.Metadata!)");
573+
methodCode.Should().NotContain("(request.Metadata!).ToString()");
574+
methodCode.Should().NotContain("(request.Labels!).ToString()");
575+
}
523576
}

0 commit comments

Comments
 (0)