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

[Multipart] Support model format for @multipartBody #1090

Merged
merged 20 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Support model format of `@multipartBody`
17 changes: 17 additions & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,28 @@ export type SdkModelPropertyType =
| SdkBodyParameter
| SdkHeaderParameter;

export interface MultipartOptionsType {
msyyc marked this conversation as resolved.
Show resolved Hide resolved
msyyc marked this conversation as resolved.
Show resolved Hide resolved
// whether this part is for file
isFilePart: boolean;
// whether this part is multi in request payload
multi: boolean;
msyyc marked this conversation as resolved.
Show resolved Hide resolved
msyyc marked this conversation as resolved.
Show resolved Hide resolved
msyyc marked this conversation as resolved.
Show resolved Hide resolved
// undefined if filename is not set explicitly in Typespec
filename?: SdkModelPropertyType;
// undefined if contentType is not set explicitly in Typespec
contentType?: SdkModelPropertyType;
msyyc marked this conversation as resolved.
Show resolved Hide resolved
// defined in Typespec or calculated by Typespec complier
defaultContentTypes: string[];
}

export interface SdkBodyModelPropertyType extends SdkModelPropertyTypeBase {
kind: "property";
discriminator: boolean;
serializedName: string;
/*
@deprecated This property is deprecated. Use `.multipartOptions?.isFilePart` instead.
*/
isMultipartFileInput: boolean;
multipartOptions?: MultipartOptionsType;
visibility?: Visibility[];
flatten: boolean;
}
Expand Down
123 changes: 97 additions & 26 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ import {
} from "@typespec/compiler";
import {
Authentication,
HttpOperationPart,
Visibility,
getAuthentication,
getHttpPart,
getServers,
isHeader,
isOrExtendsHttpFile,
isStatusCode,
} from "@typespec/http";
import {
Expand Down Expand Up @@ -855,7 +858,14 @@ export function getClientTypeWithDiagnostics(
case "Model":
retval = diagnostics.pipe(getSdkArrayOrDictWithDiagnostics(context, type, operation));
if (retval === undefined) {
retval = diagnostics.pipe(getSdkModelWithDiagnostics(context, type, operation));
const httpPart = getHttpPart(context.program, type);
msyyc marked this conversation as resolved.
Show resolved Hide resolved
msyyc marked this conversation as resolved.
Show resolved Hide resolved
if (httpPart === undefined) {
retval = diagnostics.pipe(getSdkModelWithDiagnostics(context, type, operation));
} else {
retval = diagnostics.pipe(
getClientTypeWithDiagnostics(context, httpPart.type, operation)
);
}
}
break;
case "Intrinsic":
Expand Down Expand Up @@ -1060,45 +1070,102 @@ export function getSdkModelPropertyTypeBase(
});
}

export function getSdkModelPropertyType(
function updateMultiPartInfo(
context: TCGCContext,
type: ModelProperty,
operation?: Operation
): [SdkModelPropertyType, readonly Diagnostic[]] {
base: SdkBodyModelPropertyType,
msyyc marked this conversation as resolved.
Show resolved Hide resolved
operation?: Operation,
httpOperationPart?: HttpOperationPart
): [void, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const base = diagnostics.pipe(getSdkModelPropertyTypeBase(context, type, operation));
const isBytesInput =
base.type.kind === "bytes" ||
(base.type.kind === "array" && base.type.valueType.kind === "bytes");

if (isSdkHttpParameter(context, type)) return getSdkHttpParameter(context, type, operation!);
// I'm a body model property
let operationIsMultipart = false;
if (operation) {
if (httpOperationPart) {
// body decorated with @multipartBody
const httpPart = getHttpPart(context.program, type.type);
base.multipartOptions = {
isFilePart:
isBytesInput ||
msyyc marked this conversation as resolved.
Show resolved Hide resolved
(httpPart !== undefined && isOrExtendsHttpFile(context.program, httpPart.type)),
multi: httpOperationPart.multi,
filename: httpOperationPart.filename
? diagnostics.pipe(getSdkModelPropertyType(context, httpOperationPart.filename, operation))
: undefined,
contentType: httpOperationPart.body.contentTypeProperty
? diagnostics.pipe(
getSdkModelPropertyType(context, httpOperationPart.body.contentTypeProperty, operation)
)
: undefined,
defaultContentTypes: httpOperationPart.body.contentTypes,
};
} else if (operation) {
// common body
const httpOperation = getHttpOperationWithCache(context, operation);
operationIsMultipart = Boolean(
const operationIsMultipart = Boolean(
httpOperation && httpOperation.parameters.body?.contentTypes.includes("multipart/form-data")
msyyc marked this conversation as resolved.
Show resolved Hide resolved
);
if (operationIsMultipart) {
// Currently we only recognize bytes and list of bytes as potential file inputs
if (isBytesInput && getEncode(context.program, type)) {
diagnostics.add(
createDiagnostic({
code: "encoding-multipart-bytes",
target: type,
})
);
}
base.multipartOptions = {
isFilePart: isBytesInput,
multi: base.type.kind === "array",
defaultContentTypes: [],
msyyc marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
// Currently we only recognize bytes and list of bytes as potential file inputs
const isBytesInput =
base.type.kind === "bytes" ||
(base.type.kind === "array" && base.type.valueType.kind === "bytes");
if (isBytesInput && operationIsMultipart && getEncode(context.program, type)) {
diagnostics.add(
createDiagnostic({
code: "encoding-multipart-bytes",
target: type,
})
);
if (base.multipartOptions !== undefined) {
base.isMultipartFileInput = base.multipartOptions.isFilePart;
}
return diagnostics.wrap({
if (base.multipartOptions?.multi && base.type.kind === "array") {
// for "images: T[]" or "images: HttpPart<T>[]", return type shall be "T" instead of "T[]"
base.type = base.type.valueType;
}

return diagnostics.wrap(undefined);
}

export function getSdkModelPropertyType(
context: TCGCContext,
type: ModelProperty,
operation?: Operation,
httpOperationPart?: HttpOperationPart
): [SdkModelPropertyType, readonly Diagnostic[]] {
msyyc marked this conversation as resolved.
Show resolved Hide resolved
const diagnostics = createDiagnosticCollector();
const base = diagnostics.pipe(getSdkModelPropertyTypeBase(context, type, operation));

if (isSdkHttpParameter(context, type)) return getSdkHttpParameter(context, type, operation!);
const result: SdkBodyModelPropertyType = {
...base,
kind: "property",
optional: type.optional,
visibility: getSdkVisibility(context, type),
discriminator: false,
serializedName: getPropertyNames(context, type)[1],
isMultipartFileInput: isBytesInput && operationIsMultipart,
isMultipartFileInput: false,
flatten: shouldFlattenProperty(context, type),
});
};
diagnostics.pipe(updateMultiPartInfo(context, type, result, operation, httpOperationPart));
return diagnostics.wrap(result);
}

function getHttpOperationParts(context: TCGCContext, operation?: Operation): HttpOperationPart[] {
if (operation) {
const body = getHttpOperationWithCache(context, operation).parameters.body;
if (body?.bodyKind === "multipart") {
return body.parts;
}
}
return [];
}

function addPropertiesToModelType(
Expand All @@ -1108,15 +1175,19 @@ function addPropertiesToModelType(
operation?: Operation
): [void, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
for (const property of type.properties.values()) {
const httpOpParts = getHttpOperationParts(context, operation);
msyyc marked this conversation as resolved.
Show resolved Hide resolved
for (const [index, property] of Array.from(type.properties.values()).entries()) {
if (
isStatusCode(context.program, property) ||
isNeverOrVoidType(property.type) ||
sdkType.kind !== "model"
) {
continue;
}
const clientProperty = diagnostics.pipe(getSdkModelPropertyType(context, property, operation));
const httpOpPart = httpOpParts.length > index ? httpOpParts[index] : undefined;
const clientProperty = diagnostics.pipe(
getSdkModelPropertyType(context, property, operation, httpOpPart)
);
if (sdkType.properties) {
sdkType.properties.push(clientProperty);
} else {
Expand Down
Loading
Loading