Skip to content

Commit

Permalink
tsp, set request mediaTypes from body.contentTypes (Azure#2836)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Jun 25, 2024
1 parent 180c01f commit 6ead6cd
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 12 deletions.
14 changes: 3 additions & 11 deletions typespec-extension/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ import { EmitterOptions } from "./emitter.js";
import { createPollOperationDetailsSchema, getFileDetailsSchema } from "./external-schemas.js";
import { ClientContext } from "./models.js";
import {
CONTENT_TYPE_KEY,
ORIGIN_API_VERSION,
SPECIAL_HEADER_NAMES,
cloneOperationParameter,
Expand Down Expand Up @@ -1132,16 +1131,6 @@ export class CodeModelBuilder {
if (op.convenienceApi) {
this.trackSchemaUsage(schema, { usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public] });
}

if (param.name.toLowerCase() === CONTENT_TYPE_KEY) {
let mediaTypes = ["application/json"];
if (schema instanceof ConstantSchema) {
mediaTypes = [schema.value.value.toString()];
} else if (schema instanceof SealedChoiceSchema) {
mediaTypes = schema.choices.map((it) => it.value.toString());
}
op.requests![0].protocol.http!.mediaTypes = mediaTypes;
}
}
}

Expand Down Expand Up @@ -1327,6 +1316,9 @@ export class CodeModelBuilder {
}

private processParameterBody(op: CodeModelOperation, httpOperation: HttpOperation, body: ModelProperty | Model) {
// set contentTypes to mediaTypes
op.requests![0].protocol.http!.mediaTypes = httpOperation.parameters.body!.contentTypes;

const parameters = httpOperation.operation.parameters;

const unknownRequestBody =
Expand Down
2 changes: 1 addition & 1 deletion typespec-extension/src/operation-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const SPECIAL_HEADER_NAMES = new Set([

export const ORIGIN_API_VERSION = "modelerfour:synthesized/api-version";

export const CONTENT_TYPE_KEY = "content-type";
const CONTENT_TYPE_KEY = "content-type";

// azure-core SerializerEncoding.SUPPORTED_MIME_TYPES
const SUPPORTED_MIME_TYPES = new Set<string>([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public final class OptionalAsyncClient {
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>request-header-optional</td><td>String</td><td>No</td><td>The requestHeaderOptional parameter</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public final class OptionalClient {
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>request-header-optional</td><td>String</td><td>No</td><td>The requestHeaderOptional parameter</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Response<BinaryData> putSync(@HostParam("endpoint") String endpoint,
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>request-header-optional</td><td>String</td><td>No</td><td>The requestHeaderOptional parameter</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
Expand Down Expand Up @@ -213,6 +215,8 @@ public Mono<Response<BinaryData>> putWithResponseAsync(String requestHeaderRequi
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>request-header-optional</td><td>String</td><td>No</td><td>The requestHeaderOptional parameter</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public final class EtagHeadersOptionalBodyAsyncClient {
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>If-Match</td><td>String</td><td>No</td><td>The request should only proceed if an entity matches this
* string.</td></tr>
* <tr><td>If-None-Match</td><td>String</td><td>No</td><td>The request should only proceed if no entity matches this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public final class EtagHeadersOptionalBodyClient {
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>If-Match</td><td>String</td><td>No</td><td>The request should only proceed if an entity matches this
* string.</td></tr>
* <tr><td>If-None-Match</td><td>String</td><td>No</td><td>The request should only proceed if no entity matches this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Response<BinaryData> putWithOptionalBodySync(@HostParam("endpoint") String endpo
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>If-Match</td><td>String</td><td>No</td><td>The request should only proceed if an entity matches this
* string.</td></tr>
* <tr><td>If-None-Match</td><td>String</td><td>No</td><td>The request should only proceed if no entity matches this
Expand Down Expand Up @@ -171,6 +173,8 @@ public Mono<Response<BinaryData>> putWithOptionalBodyWithResponseAsync(String fo
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* <tr><td>If-Match</td><td>String</td><td>No</td><td>The request should only proceed if an entity matches this
* string.</td></tr>
* <tr><td>If-None-Match</td><td>String</td><td>No</td><td>The request should only proceed if no entity matches this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public final class OptionalExplicitAsyncClient {

/**
* The set operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand All @@ -63,6 +71,14 @@ public Mono<Response<Void>> setWithResponse(RequestOptions requestOptions) {

/**
* The omit operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public final class OptionalExplicitClient {

/**
* The set operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand All @@ -61,6 +69,14 @@ public Response<Void> setWithResponse(RequestOptions requestOptions) {

/**
* The omit operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public interface OptionalExplicitsService {

/**
* The set operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand Down Expand Up @@ -120,6 +128,14 @@ public Mono<Response<Void>> setWithResponseAsync(RequestOptions requestOptions)

/**
* The set operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand Down Expand Up @@ -149,6 +165,14 @@ public Response<Void> setWithResponse(RequestOptions requestOptions) {

/**
* The omit operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand Down Expand Up @@ -178,6 +202,14 @@ public Mono<Response<Void>> omitWithResponseAsync(RequestOptions requestOptions)

/**
* The omit operation.
* <p><strong>Header Parameters</strong></p>
* <table border="1">
* <caption>Header Parameters</caption>
* <tr><th>Name</th><th>Type</th><th>Required</th><th>Description</th></tr>
* <tr><td>Content-Type</td><td>String</td><td>No</td><td>The content type. Allowed values:
* "application/json".</td></tr>
* </table>
* You can add these to a request with {@link RequestOptions#addHeader}
* <p><strong>Request Body Schema</strong></p>
*
* <pre>{@code
Expand Down

0 comments on commit 6ead6cd

Please sign in to comment.