Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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:
- "@typespec/http-client-java"
---

Support File from TypeSpec.
33 changes: 23 additions & 10 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ import { createPollOperationDetailsSchema, getFileDetailsSchema } from "./extern
import { createDiagnostic, reportDiagnostic } from "./lib.js";
import { ClientContext } from "./models.js";
import {
CONTENT_TYPE_KEY,
ORIGIN_API_VERSION,
SPECIAL_HEADER_NAMES,
cloneOperationParameter,
findResponsePropertySegments,
getServiceVersion,
isContentTypeHeader,
isKnownContentType,
isLroNewPollingStrategy,
operationIsJsonMergePatch,
Expand Down Expand Up @@ -1002,14 +1002,14 @@ export class CodeModelBuilder {
httpOperation.bodyParam &&
param.kind === "header"
) {
if (param.serializedName.toLocaleLowerCase() === CONTENT_TYPE_KEY) {
if (isContentTypeHeader(param)) {
continue;
}
}
// if the request body is optional, skip content-type header added by TCGC
// TODO: add optional content type to code-model, and support optional content-type from codegen, https://github.com/Azure/autorest.java/issues/2930
if (httpOperation.bodyParam && httpOperation.bodyParam.optional) {
if (param.serializedName.toLocaleLowerCase() === CONTENT_TYPE_KEY) {
if (isContentTypeHeader(param)) {
continue;
}
}
Expand Down Expand Up @@ -2199,7 +2199,7 @@ export class CodeModelBuilder {

if (schema instanceof ConstantSchema) {
// skip constant header in response
if (header.serializedName.toLowerCase() !== "content-type") {
if (isContentTypeHeader(header)) {
// we does not warn on content-type as constant, as this is the most common case
reportDiagnostic(this.program, {
code: "constant-header-in-response-removed",
Expand All @@ -2226,14 +2226,27 @@ export class CodeModelBuilder {
const bodyType: SdkType | undefined = sdkResponse.type;
let trackConvenienceApi: boolean = Boolean(op.convenienceApi);

const unknownResponseBody =
sdkResponse.contentTypes &&
sdkResponse.contentTypes.length > 0 &&
!isKnownContentType(sdkResponse.contentTypes);
let responseIsFile: boolean = false;
if (
bodyType &&
bodyType.kind === "model" &&
bodyType.serializationOptions.binary &&
bodyType.serializationOptions.binary.isFile
) {
// check for File
responseIsFile = true;
} else if (bodyType && bodyType.kind === "bytes") {
// check for bytes + unknown content-type
const unknownResponseBody =
sdkResponse.contentTypes &&
sdkResponse.contentTypes.length > 0 &&
!isKnownContentType(sdkResponse.contentTypes);
responseIsFile = Boolean(unknownResponseBody);
}

let response: Response;
if (unknownResponseBody && bodyType && bodyType.kind === "bytes") {
// binary
if (responseIsFile) {
// binary/file
response = new BinaryResponse({
protocol: {
http: {
Expand Down
22 changes: 21 additions & 1 deletion packages/http-client-java/emitter/src/operation-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { ObjectSchema, Parameter, Property, SchemaResponse } from "@autorest/codemodel";
import {
SdkCookieParameter,
SdkHeaderParameter,
SdkHttpOperation,
SdkLroServiceMetadata,
SdkModelPropertyType,
SdkPathParameter,
SdkQueryParameter,
SdkServiceResponseHeader,
} from "@azure-tools/typespec-client-generator-core";
import { Operation, Program, Type, Union } from "@typespec/compiler";
Expand All @@ -24,7 +28,8 @@ 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";
const CONTENT_TYPE_NAME = "contentType";

// azure-core SerializerEncoding.SUPPORTED_MIME_TYPES
const SUPPORTED_MIME_TYPES = new Set<string>([
Expand All @@ -40,6 +45,21 @@ const SUPPORTED_MIME_TYPES = new Set<string>([
"application/merge-patch+json",
]);

export function isContentTypeHeader(
header:
| SdkPathParameter
| SdkQueryParameter
| SdkHeaderParameter
| SdkCookieParameter
| SdkServiceResponseHeader,
): boolean {
return (
(header.serializedName && header.serializedName.toLowerCase() === CONTENT_TYPE_KEY) ||
// TODO: remove after TCGC bug fix
(!header.serializedName && header.name === CONTENT_TYPE_NAME)
);
}

export function isKnownContentType(contentTypes: string[]): boolean {
return contentTypes
.map((it) => it.toLowerCase())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"@typespec/sse": "0.78.0",
"@typespec/streams": "0.78.0",
"@azure-tools/typespec-azure-core": "0.64.0",
"@azure-tools/typespec-client-generator-core": "0.64.4",
"@azure-tools/typespec-azure-resource-manager": "0.64.0",
"@azure-tools/typespec-autorest": "0.64.0"
"@azure-tools/typespec-client-generator-core": "0.64.5",
"@azure-tools/typespec-azure-resource-manager": "0.64.1",
"@azure-tools/typespec-autorest": "0.64.1"
},
"private": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.57.0</version>
<version>1.57.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public enum Dependency {
// azure
AZURE_CLIENT_SDK_PARENT("com.azure", "azure-client-sdk-parent", "1.7.0"),
AZURE_CLIENT_SDK_PARENT_V2("com.azure.v2", "azure-client-sdk-parent", "2.0.0-beta.1"),
AZURE_CORE("com.azure", "azure-core", "1.57.0"),
AZURE_CORE("com.azure", "azure-core", "1.57.1"),
AZURE_CORE_V2("com.azure.v2", "azure-core", "2.0.0-beta.1"),
AZURE_CORE_MANAGEMENT("com.azure", "azure-core-management", "1.19.2"),
AZURE_CORE_HTTP_NETTY("com.azure", "azure-core-http-netty", "1.16.2"),
AZURE_CORE_TEST("com.azure", "azure-core-test", "1.27.0-beta.13"),
AZURE_IDENTITY("com.azure", "azure-identity", "1.18.1"),
AZURE_CORE_EXPERIMENTAL("com.azure", "azure-core-experimental", "1.0.0-beta.65"),
AZURE_CORE_MANAGEMENT("com.azure", "azure-core-management", "1.19.3"),
AZURE_CORE_HTTP_NETTY("com.azure", "azure-core-http-netty", "1.16.3"),
AZURE_CORE_TEST("com.azure", "azure-core-test", "1.27.0-beta.14"),
AZURE_IDENTITY("com.azure", "azure-identity", "1.18.2"),
AZURE_CORE_EXPERIMENTAL("com.azure", "azure-core-experimental", "1.0.0-beta.66"),

CLIENTCORE("io.clientcore", "core", "1.0.0-beta.11");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,29 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-json</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-xml</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.57.0</version>
<version>1.57.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.16.2</version>
<version>1.16.3</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.27.0-beta.13</version>
<version>1.27.0-beta.14</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-management</artifactId>
<version>1.19.2</version>
<version>1.19.3</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"@typespec/sse": "0.78.0",
"@typespec/streams": "0.78.0",
"@azure-tools/typespec-azure-core": "0.64.0",
"@azure-tools/typespec-client-generator-core": "0.64.4",
"@azure-tools/typespec-azure-resource-manager": "0.64.0",
"@azure-tools/typespec-autorest": "0.64.0"
"@azure-tools/typespec-client-generator-core": "0.64.5",
"@azure-tools/typespec-azure-resource-manager": "0.64.1",
"@azure-tools/typespec-autorest": "0.64.1"
},
"private": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.57.0</version>
<version>1.57.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-json</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-xml</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.27.0-beta.13</version>
<version>1.27.0-beta.14</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand All @@ -50,12 +50,12 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.18.1</version>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-management</artifactId>
<version>1.19.2</version>
<version>1.19.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public final class StorageAccountsClientImpl implements StorageAccountsClient {
@ServiceInterface(name = "CombinedStorageAccounts")
public interface StorageAccountsService {
@Headers({ "Content-Type: application/json" })
@Get("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/storageAccounts/{accountName}")
@Get("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}")
@ExpectedResponses({ 200 })
@UnexpectedResponseExceptionType(ManagementException.class)
Mono<Response<StorageAccountInner>> getByResourceGroup(@HostParam("endpoint") String endpoint,
Expand All @@ -75,15 +75,15 @@ Mono<Response<StorageAccountInner>> getByResourceGroup(@HostParam("endpoint") St
@HeaderParam("Accept") String accept, Context context);

@Headers({ "Content-Type: application/json" })
@Get("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/storageAccounts/{accountName}")
@Get("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}")
@ExpectedResponses({ 200 })
@UnexpectedResponseExceptionType(ManagementException.class)
Response<StorageAccountInner> getByResourceGroupSync(@HostParam("endpoint") String endpoint,
@QueryParam("api-version") String apiVersion, @PathParam("subscriptionId") String subscriptionId,
@PathParam("resourceGroupName") String resourceGroupName, @PathParam("accountName") String accountName,
@HeaderParam("Accept") String accept, Context context);

@Put("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/storageAccounts/{accountName}")
@Put("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}")
@ExpectedResponses({ 200, 201 })
@UnexpectedResponseExceptionType(ManagementException.class)
Mono<Response<Flux<ByteBuffer>>> createOrUpdate(@HostParam("endpoint") String endpoint,
Expand All @@ -92,7 +92,7 @@ Mono<Response<Flux<ByteBuffer>>> createOrUpdate(@HostParam("endpoint") String en
@HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept,
@BodyParam("application/json") StorageAccountInner resource, Context context);

@Put("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/storageAccounts/{accountName}")
@Put("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}")
@ExpectedResponses({ 200, 201 })
@UnexpectedResponseExceptionType(ManagementException.class)
Response<BinaryData> createOrUpdateSync(@HostParam("endpoint") String endpoint,
Expand Down
Loading
Loading