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
9 changes: 5 additions & 4 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,9 @@ export class CodeModelBuilder {
const versions = getServiceApiVersions(this.program, client);
if (versions && versions.length > 0) {
if (!this.sdkContext.apiVersion || ["all", "latest"].includes(this.sdkContext.apiVersion)) {
this.apiVersion = versions[versions.length - 1];
this.apiVersion = versions[versions.length - 1].value;
} else {
this.apiVersion = versions.find((it: string) => it === this.sdkContext.apiVersion);
this.apiVersion = versions.find((it) => it.value === this.sdkContext.apiVersion)?.value;
if (!this.apiVersion) {
reportDiagnostic(this.program, {
code: "invalid-api-version",
Expand All @@ -667,12 +667,13 @@ export class CodeModelBuilder {

codeModelClient.apiVersions = [];
for (const version of getFilteredApiVersions(
this.program,
this.apiVersion,
versions,
this.options["service-version-exclude-preview"],
)) {
const apiVersion = new ApiVersion();
apiVersion.version = version;
apiVersion.version = version.value;
codeModelClient.apiVersions.push(apiVersion);
}
}
Expand Down Expand Up @@ -1835,7 +1836,7 @@ export class CodeModelBuilder {
}

let schemaName = groupToRequestConditions ? "RequestConditions" : "MatchConditions";
if (!this.isBranded()) {
if (!this.isAzureV1()) {
schemaName = "Http" + schemaName;
}
const schemaDescription = groupToRequestConditions
Expand Down
36 changes: 20 additions & 16 deletions packages/http-client-java/emitter/src/versioning-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isPreviewVersion } from "@azure-tools/typespec-azure-core";
import { SdkClientType, SdkHttpOperation } from "@azure-tools/typespec-client-generator-core";
import { Namespace, Program } from "@typespec/compiler";
import { findVersionedNamespace, getVersions } from "@typespec/versioning";
import { findVersionedNamespace, getVersions, Version } from "@typespec/versioning";

/**
* Gets the array of api-version on the TypeSpec service that contains this SDK client.
Expand All @@ -13,46 +14,49 @@ import { findVersionedNamespace, getVersions } from "@typespec/versioning";
export function getServiceApiVersions(
program: Program,
client: SdkClientType<SdkHttpOperation>,
): string[] | undefined {
): Version[] | undefined {
// TODO: use client.apiVersions after TCGC supports multiple service
// Also, this function lacks the logic of the handling of added/removed on the Namespace/Interface of the SDK client.
let apiVersions: string[] | undefined;
let apiVersions: Version[] | undefined;
const versionedNamespace: Namespace | undefined = findVersionedNamespace(
program,
client.__raw.service,
);
if (versionedNamespace) {
apiVersions = getVersions(program, versionedNamespace)[1]
?.getVersions()
.map((version) => version.value);
apiVersions = getVersions(program, versionedNamespace)[1]?.getVersions();
}
return apiVersions;
}

/**
* Filter api-versions for "ServiceVersion".
* TODO(xiaofei) pending TCGC design: https://github.com/Azure/typespec-azure/issues/965
* We still cannot move to TCGC, due to it only recognizes api-versions from 1 service.
*
* @param pinnedApiVersion the api-version to use as filter base
* @param versions api-versions to filter
* @param excludePreview whether to exclude preview api-versions when pinnedApiVersion is stable, default is `true`
* @returns filtered api-versions
*/
export function getFilteredApiVersions(
program: Program,
pinnedApiVersion: string | undefined,
versions: string[],
excludePreview: boolean = false,
): string[] {
versions: Version[],
excludePreview: boolean = true,
): Version[] {
if (!pinnedApiVersion) {
return versions;
}
const filterPreviewApiVersions = excludePreview && isStableApiVersionString(pinnedApiVersion);
return versions
.slice(0, versions.indexOf(pinnedApiVersion) + 1)
.filter(
(version) =>
!excludePreview || !isStableApiVersion(pinnedApiVersion) || isStableApiVersion(version),
);
.slice(0, versions.findIndex((it) => it.value === pinnedApiVersion) + 1)
.filter((version) => !filterPreviewApiVersions || isStableApiVersion(program, version));
}

function isStableApiVersion(program: Program, version: Version): boolean {
return !isPreviewVersion(program, version.enumMember) && isStableApiVersionString(version.value);
}

export function isStableApiVersion(version: string): boolean {
return !version.toLowerCase().includes("preview");
export function isStableApiVersionString(version: string): boolean {
return !version.toLowerCase().endsWith("-preview");
}
6 changes: 3 additions & 3 deletions packages/http-client-java/emitter/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
removeClientSuffix,
stringArrayContainsIgnoreCase,
} from "../src/utils.js";
import { isStableApiVersion } from "../src/versioning-utils.js";
import { isStableApiVersionString } from "../src/versioning-utils.js";

describe("utils", () => {
it("pascalCase", () => {
Expand Down Expand Up @@ -39,8 +39,8 @@ describe("utils", () => {

describe("versioning-utils", () => {
it("isStableApiVersion", () => {
expect(isStableApiVersion("2022-09-01")).toBe(true);
expect(isStableApiVersion("2023-12-01-preview")).toBe(false);
expect(isStableApiVersionString("2022-09-01")).toBe(true);
expect(isStableApiVersionString("2023-12-01-preview")).toBe(false);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@
"spector-stop": "tsp-spector server stop"
},
"dependencies": {
"@typespec/spector": "0.1.0-alpha.18",
"@typespec/spector": "0.1.0-alpha.19",
"@typespec/spec-api": "0.1.0-alpha.9",
"@typespec/http-specs": "0.1.0-alpha.26",
"@typespec/json-schema": "1.4.0",
"@typespec/http-specs": "0.1.0-alpha.27",
"@typespec/json-schema": "1.5.0",
"@typespec/http-client-java": "file:../../typespec-http-client-java-0.4.3.tgz",
"@typespec/http-client-java-tests": "file:"
},
"overrides": {
"@typespec/compiler": "1.4.0",
"@typespec/http": "1.4.0",
"@typespec/rest": "0.74.0",
"@typespec/versioning": "0.74.0",
"@typespec/openapi": "1.4.0",
"@typespec/xml": "0.74.0",
"@typespec/events": "0.74.0",
"@typespec/sse": "0.74.0",
"@typespec/streams": "0.74.0",
"@azure-tools/typespec-azure-core": "0.60.0",
"@azure-tools/typespec-client-generator-core": "0.60.3",
"@azure-tools/typespec-azure-resource-manager": "0.60.1",
"@azure-tools/typespec-autorest": "0.60.0"
"@typespec/compiler": "1.5.0",
"@typespec/http": "1.5.0",
"@typespec/rest": "0.75.0",
"@typespec/versioning": "0.75.0",
"@typespec/openapi": "1.5.0",
"@typespec/xml": "0.75.0",
"@typespec/events": "0.75.0",
"@typespec/sse": "0.75.0",
"@typespec/streams": "0.75.0",
"@azure-tools/typespec-azure-core": "0.61.0",
"@azure-tools/typespec-client-generator-core": "0.61.0",
"@azure-tools/typespec-azure-resource-manager": "0.61.0",
"@azure-tools/typespec-autorest": "0.61.0"
},
"private": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,129 @@ public Response<Void> float64SecondsWithResponse(Duration duration, RequestConte
public void float64Seconds(Duration duration) {
float64SecondsWithResponse(duration, RequestContext.none());
}

/**
* The int32Milliseconds operation.
*
* @param duration The duration parameter.
* @param requestContext The context to configure the HTTP request before HTTP client sends it.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<Void> int32MillisecondsWithResponse(int duration, RequestContext requestContext) {
return this.instrumentation.instrumentWithResponse("Encode.Duration.Header.int32Milliseconds", requestContext,
updatedContext -> this.serviceClient.int32MillisecondsWithResponse(duration, updatedContext));
}

/**
* The int32Milliseconds operation.
*
* @param duration The duration parameter.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public void int32Milliseconds(int duration) {
int32MillisecondsWithResponse(duration, RequestContext.none());
}

/**
* The floatMilliseconds operation.
*
* @param duration The duration parameter.
* @param requestContext The context to configure the HTTP request before HTTP client sends it.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<Void> floatMillisecondsWithResponse(double duration, RequestContext requestContext) {
return this.instrumentation.instrumentWithResponse("Encode.Duration.Header.floatMilliseconds", requestContext,
updatedContext -> this.serviceClient.floatMillisecondsWithResponse(duration, updatedContext));
}

/**
* The floatMilliseconds operation.
*
* @param duration The duration parameter.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public void floatMilliseconds(double duration) {
floatMillisecondsWithResponse(duration, RequestContext.none());
}

/**
* The float64Milliseconds operation.
*
* @param duration The duration parameter.
* @param requestContext The context to configure the HTTP request before HTTP client sends it.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<Void> float64MillisecondsWithResponse(double duration, RequestContext requestContext) {
return this.instrumentation.instrumentWithResponse("Encode.Duration.Header.float64Milliseconds", requestContext,
updatedContext -> this.serviceClient.float64MillisecondsWithResponse(duration, updatedContext));
}

/**
* The float64Milliseconds operation.
*
* @param duration The duration parameter.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public void float64Milliseconds(double duration) {
float64MillisecondsWithResponse(duration, RequestContext.none());
}

/**
* The int32MillisecondsArray operation.
*
* @param duration The duration parameter.
* @param requestContext The context to configure the HTTP request before HTTP client sends it.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<Void> int32MillisecondsArrayWithResponse(List<Integer> duration, RequestContext requestContext) {
return this.instrumentation.instrumentWithResponse("Encode.Duration.Header.int32MillisecondsArray",
requestContext,
updatedContext -> this.serviceClient.int32MillisecondsArrayWithResponse(duration, updatedContext));
}

/**
* The int32MillisecondsArray operation.
*
* @param duration The duration parameter.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@Metadata(properties = { MetadataProperties.GENERATED })
@ServiceMethod(returns = ReturnType.SINGLE)
public void int32MillisecondsArray(List<Integer> duration) {
int32MillisecondsArrayWithResponse(duration, RequestContext.none());
}
}
Loading
Loading