Skip to content

Commit 7c5aabe

Browse files
archerzzMingzhe Huang (from Dev Box)
andauthored
refactor(http-client-csharp): remove BodyMediaType (#6119)
- remove `BodyMediaType` - remove corresponding properties in input models: - `InputOperation.RequestBodyMediaType` - `OperationResponse.BodyMediaType` - regen test/sample part of #4215 --------- Co-authored-by: Mingzhe Huang (from Dev Box) <mingzhehuang@microsoft.com>
1 parent e262599 commit 7c5aabe

File tree

77 files changed

+3
-1573
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+3
-1573
lines changed

packages/http-client-csharp/emitter/src/lib/operation-converter.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { getDeprecated, isErrorModel, NoTarget } from "@typespec/compiler";
1919
import { HttpStatusCodeRange } from "@typespec/http";
2020
import { getResourceOperation } from "@typespec/rest";
2121
import { CSharpEmitterContext } from "../sdk-context.js";
22-
import { BodyMediaType } from "../type/body-media-type.js";
2322
import { collectionFormatToDelimMap } from "../type/collection-format.js";
2423
import { HttpResponseHeader } from "../type/http-response-header.js";
2524
import { InputConstant } from "../type/input-constant.js";
@@ -71,7 +70,6 @@ export function fromSdkServiceMethod(
7170
Parameters: [...parameterMap.values()],
7271
Responses: [...responseMap.values()],
7372
HttpMethod: parseHttpRequestMethod(method.operation.verb),
74-
RequestBodyMediaType: getBodyMediaType(method.operation.bodyParam?.type),
7573
Uri: uri,
7674
Path: method.operation.path,
7775
ExternalDocsUrl: getExternalDocs(sdkContext, method.operation.__raw.operation)?.url,
@@ -221,7 +219,6 @@ function loadLongRunningOperation(
221219
method.lroMetadata.finalResponse?.envelopeResult !== undefined
222220
? fromSdkModelType(sdkContext, method.lroMetadata.finalResponse.envelopeResult)
223221
: undefined,
224-
BodyMediaType: BodyMediaType.Json,
225222
} as OperationResponse,
226223
ResultPath: method.lroMetadata.finalResponse?.resultPath,
227224
};
@@ -237,7 +234,6 @@ function fromSdkHttpOperationResponses(
237234
responses.set(r, {
238235
StatusCodes: toStatusCodesArray(range),
239236
BodyType: r.type ? fromSdkType(sdkContext, r.type) : undefined,
240-
BodyMediaType: BodyMediaType.Json,
241237
Headers: fromSdkServiceResponseHeaders(sdkContext, r.headers),
242238
IsErrorResponse: r.type !== undefined && isErrorModel(sdkContext.program, r.type.__raw!),
243239
ContentTypes: r.contentTypes,
@@ -272,21 +268,6 @@ function toStatusCodesArray(range: number | HttpStatusCodeRange): number[] {
272268
return statusCodes;
273269
}
274270

275-
function getBodyMediaType(type: SdkType | undefined) {
276-
if (type === undefined) {
277-
return BodyMediaType.None;
278-
}
279-
280-
if (type.kind === "model") {
281-
return BodyMediaType.Json;
282-
} else if (type.kind === "string") {
283-
return BodyMediaType.Text;
284-
} else if (type.kind === "bytes") {
285-
return BodyMediaType.Binary;
286-
}
287-
return BodyMediaType.None;
288-
}
289-
290271
function getRequestMediaTypes(op: SdkHttpOperation): string[] | undefined {
291272
const contentTypes = op.parameters.filter(
292273
(p) => p.kind === "header" && p.serializedName.toLocaleLowerCase() === "content-type",

packages/http-client-csharp/emitter/src/type/body-media-type.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/http-client-csharp/emitter/src/type/input-operation.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
import { DecoratorInfo } from "@azure-tools/typespec-client-generator-core";
5-
import { BodyMediaType } from "./body-media-type.js";
65
import { InputHttpOperationExample } from "./input-examples.js";
76
import { InputParameter } from "./input-parameter.js";
87
import { OperationLongRunning } from "./operation-long-running.js";
@@ -26,7 +25,6 @@ export interface InputOperation {
2625
Parameters: InputParameter[];
2726
Responses: OperationResponse[];
2827
HttpMethod: RequestMethod;
29-
RequestBodyMediaType: BodyMediaType;
3028
Uri: string;
3129
Path: string;
3230
ExternalDocsUrl?: string;

packages/http-client-csharp/emitter/src/type/operation-response.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
import { BodyMediaType } from "./body-media-type.js";
54
import { HttpResponseHeader } from "./http-response-header.js";
65
import { InputType } from "./input-type.js";
76

87
export interface OperationResponse {
98
StatusCodes: number[];
109
BodyType?: InputType;
11-
BodyMediaType: BodyMediaType;
1210
Headers: HttpResponseHeader[];
1311
ContentTypes?: string[];
1412
IsErrorResponse: boolean;

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Utilities/FormattableStringHelpers.cs

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,6 @@ namespace Microsoft.TypeSpec.Generator.ClientModel
1010
{
1111
internal static class FormattableStringHelpers
1212
{
13-
private static readonly Regex ContentTypeRegex = new Regex(@"(application|audio|font|example|image|message|model|multipart|text|video|x-(?:[0-9A-Za-z!#$%&'*+.^_`|~-]+))\/([0-9A-Za-z!#$%&'*.^_`|~-]+)\s*(?:\+([0-9A-Za-z!#$%&'*.^_`|~-]+))?\s*(?:;.\s*(\S*))?", RegexOptions.Compiled);
14-
15-
public static BodyMediaType ToMediaType(string contentType)
16-
{
17-
var matches = ContentTypeRegex.Matches(contentType);
18-
if (matches.Count == 0)
19-
{
20-
throw new NotSupportedException($"Content type {contentType} is not supported.");
21-
}
22-
23-
var type = matches[0].Groups[1].Value;
24-
var subType = matches[0].Groups[2].Value;
25-
var suffix = matches[0].Groups[3].Value;
26-
var parameter = matches[0].Groups[4].Value;
27-
28-
var typeSubs = contentType.Split('/');
29-
if (typeSubs.Length != 2)
30-
{
31-
throw new NotSupportedException($"Content type {contentType} is not supported.");
32-
}
33-
34-
if ((subType == "json" || suffix == "json") && (type == "application" || type == "text") && suffix == "" && parameter == "")
35-
{
36-
return BodyMediaType.Json;
37-
}
38-
39-
if ((subType == "xml" || suffix == "xml") && (type == "application" || type == "text"))
40-
{
41-
return BodyMediaType.Xml;
42-
}
43-
44-
if (type == "audio" || type == "image" || type == "video" || subType == "octet-stream" || parameter == "serialization=Avro")
45-
{
46-
return BodyMediaType.Binary;
47-
}
48-
49-
if (type == "application" && subType == "formEncoded")
50-
{
51-
return BodyMediaType.Form;
52-
}
53-
54-
if (type == "multipart" && subType == "form-data")
55-
{
56-
return BodyMediaType.Multipart;
57-
}
58-
59-
if (type == "application")
60-
{
61-
return BodyMediaType.Binary;
62-
}
63-
64-
if (type == "text")
65-
{
66-
return BodyMediaType.Text;
67-
}
68-
69-
throw new NotSupportedException($"Content type {contentType} is not supported.");
70-
}
71-
7213
public static FormattableString Empty => $"";
7314

7415
[return: NotNullIfNotNull(nameof(s))]

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/BodyMediaType.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperation.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public InputOperation(
2121
IReadOnlyList<InputParameter> parameters,
2222
IReadOnlyList<OperationResponse> responses,
2323
string httpMethod,
24-
BodyMediaType requestBodyMediaType,
2524
string uri,
2625
string path,
2726
string? externalDocsUrl,
@@ -42,7 +41,6 @@ public InputOperation(
4241
Parameters = parameters;
4342
Responses = responses;
4443
HttpMethod = httpMethod;
45-
RequestBodyMediaType = requestBodyMediaType;
4644
Uri = uri;
4745
Path = path;
4846
ExternalDocsUrl = externalDocsUrl;
@@ -65,7 +63,6 @@ public InputOperation() : this(
6563
parameters: Array.Empty<InputParameter>(),
6664
responses: Array.Empty<OperationResponse>(),
6765
httpMethod: string.Empty,
68-
requestBodyMediaType: BodyMediaType.None,
6966
uri: string.Empty,
7067
path: string.Empty,
7168
externalDocsUrl: null,
@@ -87,7 +84,6 @@ public InputOperation() : this(
8784
public IReadOnlyList<InputParameter> Parameters { get; internal set; }
8885
public IReadOnlyList<OperationResponse> Responses { get; internal set; }
8986
public string HttpMethod { get; internal set; }
90-
public BodyMediaType RequestBodyMediaType { get; internal set; }
9187
public string Uri { get; internal set; }
9288
public string Path { get; internal set; }
9389
public string? ExternalDocsUrl { get; internal set; }

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/OperationResponse.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@ namespace Microsoft.TypeSpec.Generator.Input
1111
/// </summary>
1212
public sealed class OperationResponse
1313
{
14-
public OperationResponse(IReadOnlyList<int> statusCodes, InputType? bodyType, BodyMediaType bodyMediaType, IReadOnlyList<OperationResponseHeader> headers, bool isErrorResponse, IReadOnlyList<string> contentTypes)
14+
public OperationResponse(IReadOnlyList<int> statusCodes, InputType? bodyType, IReadOnlyList<OperationResponseHeader> headers, bool isErrorResponse, IReadOnlyList<string> contentTypes)
1515
{
1616
StatusCodes = statusCodes;
1717
BodyType = bodyType;
18-
BodyMediaType = bodyMediaType;
1918
Headers = headers;
2019
IsErrorResponse = isErrorResponse;
2120
ContentTypes = contentTypes;
2221
}
2322

24-
public OperationResponse() : this(Array.Empty<int>(), null, BodyMediaType.None, Array.Empty<OperationResponseHeader>(), false, Array.Empty<string>()) { }
23+
public OperationResponse() : this(Array.Empty<int>(), null, Array.Empty<OperationResponseHeader>(), false, Array.Empty<string>()) { }
2524

2625
public IReadOnlyList<int> StatusCodes { get; }
2726
public InputType? BodyType { get; }
28-
public BodyMediaType BodyMediaType { get; }
2927
public IReadOnlyList<OperationResponseHeader> Headers { get; }
3028
public bool IsErrorResponse { get; }
3129
public IReadOnlyList<string> ContentTypes { get; }

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/TypeSpecInputOperationConverter.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public override void Write(Utf8JsonWriter writer, InputOperation value, JsonSeri
4545
IReadOnlyList<InputParameter>? parameters = null;
4646
IReadOnlyList<OperationResponse>? responses = null;
4747
string? httpMethod = null;
48-
BodyMediaType requestBodyMediaType = default;
4948
string? uri = null;
5049
string? path = null;
5150
string? externalDocsUrl = null;
@@ -69,7 +68,6 @@ public override void Write(Utf8JsonWriter writer, InputOperation value, JsonSeri
6968
|| reader.TryReadWithConverter(nameof(InputOperation.Parameters), options, ref parameters)
7069
|| reader.TryReadWithConverter(nameof(InputOperation.Responses), options, ref responses)
7170
|| reader.TryReadString(nameof(InputOperation.HttpMethod), ref httpMethod)
72-
|| reader.TryReadWithConverter(nameof(InputOperation.RequestBodyMediaType), options, ref requestBodyMediaType)
7371
|| reader.TryReadString(nameof(InputOperation.Uri), ref uri)
7472
|| reader.TryReadString(nameof(InputOperation.Path), ref path)
7573
|| reader.TryReadString(nameof(InputOperation.ExternalDocsUrl), ref externalDocsUrl)
@@ -97,7 +95,6 @@ public override void Write(Utf8JsonWriter writer, InputOperation value, JsonSeri
9795
operation.Parameters = parameters ?? Array.Empty<InputParameter>();
9896
operation.Responses = responses ?? Array.Empty<OperationResponse>();
9997
operation.HttpMethod = httpMethod ?? throw new JsonException("InputOperation must have HttpMethod");
100-
operation.RequestBodyMediaType = requestBodyMediaType;
10198
operation.Uri = uri ?? throw new JsonException("InputOperation must have Uri");
10299
operation.Path = path ?? throw new JsonException("InputOperation must have Path");
103100
operation.ExternalDocsUrl = externalDocsUrl;

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/TypeSpecOperationResponseConverter.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ private OperationResponse CreateOperationResponse(ref Utf8JsonReader reader, str
3030
var isFirstProperty = id == null;
3131
IReadOnlyList<int>? statusCodes = null;
3232
InputType? bodyType = null;
33-
string? bodyMediaTypeString = null;
3433
IReadOnlyList<OperationResponseHeader>? headers = null;
3534
bool isErrorResponse = default;
3635
IReadOnlyList<string>? contentTypes = null;
@@ -39,7 +38,6 @@ private OperationResponse CreateOperationResponse(ref Utf8JsonReader reader, str
3938
var isKnownProperty = reader.TryReadReferenceId(ref isFirstProperty, ref id)
4039
|| reader.TryReadWithConverter(nameof(OperationResponse.StatusCodes), options, ref statusCodes)
4140
|| reader.TryReadWithConverter(nameof(OperationResponse.BodyType), options, ref bodyType)
42-
|| reader.TryReadString(nameof(OperationResponse.BodyMediaType), ref bodyMediaTypeString)
4341
|| reader.TryReadWithConverter(nameof(OperationResponse.Headers), options, ref headers)
4442
|| reader.TryReadBoolean(nameof(OperationResponse.IsErrorResponse), ref isErrorResponse)
4543
|| reader.TryReadWithConverter(nameof(OperationResponse.ContentTypes), options, ref contentTypes);
@@ -54,12 +52,7 @@ private OperationResponse CreateOperationResponse(ref Utf8JsonReader reader, str
5452
contentTypes ??= [];
5553
headers ??= [];
5654

57-
if (!Enum.TryParse<BodyMediaType>(bodyMediaTypeString, true, out var bodyMediaType))
58-
{
59-
throw new JsonException();
60-
}
61-
62-
var result = new OperationResponse(statusCodes, bodyType, bodyMediaType, headers, isErrorResponse, contentTypes);
55+
var result = new OperationResponse(statusCodes, bodyType, headers, isErrorResponse, contentTypes);
6356

6457
if (id != null)
6558
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/common/InputFactory.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ public static InputOperation Operation(
232232
parameters is null ? [] : [.. parameters],
233233
responses is null ? [OperationResponse()] : [.. responses],
234234
httpMethod,
235-
BodyMediaType.Json,
236235
uri,
237236
path,
238237
null,
@@ -250,7 +249,6 @@ public static OperationResponse OperationResponse(IEnumerable<int>? statusCodes
250249
return new OperationResponse(
251250
statusCodes is null ? [200] : [.. statusCodes],
252251
bodytype,
253-
BodyMediaType.Json,
254252
[],
255253
false,
256254
["application/json"]);

0 commit comments

Comments
 (0)