Skip to content

Commit

Permalink
dpg, make ServiceVersion only contain api-versions no later than th…
Browse files Browse the repository at this point in the history
…e pinned one (#2724)

* add ServiceVersion filter

* re-generate test code

* format

* version sort

* bump version

---------

Co-authored-by: actions-user <[email protected]>
  • Loading branch information
XiaofeiCao and actions-user committed Apr 28, 2024
1 parent 7103f43 commit 5a6afa3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 49 deletions.
6 changes: 6 additions & 0 deletions typespec-extension/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History

## 0.15.14 (2024-04-27)

Compatible with compiler 0.55.

- Added `ServiceVersion` filter for pinned api-version.

## 0.15.13 (2024-04-26)

Compatible with compiler 0.55.
Expand Down
4 changes: 2 additions & 2 deletions typespec-extension/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typespec-extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/typespec-java",
"version": "0.15.13",
"version": "0.15.14",
"description": "TypeSpec library for emitting Java client from the TypeSpec REST protocol binding",
"keywords": [
"TypeSpec"
Expand Down
62 changes: 29 additions & 33 deletions typespec-extension/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
isPathParam,
HttpOperationBody,
} from "@typespec/http";
import { Availability, Version, getAddedOnVersions, getAvailabilityMap, getVersion } from "@typespec/versioning";
import { Version, getAddedOnVersions, getVersion } from "@typespec/versioning";
import {
isPollingLocation,
getPagedResult,
Expand Down Expand Up @@ -164,6 +164,7 @@ import {
modelIs,
getNamePrefixForProperty,
isAllValueInteger,
isStable,
} from "./type-utils.js";
import {
getServiceVersion,
Expand Down Expand Up @@ -558,13 +559,6 @@ export class CodeModelBuilder {
const versioning = getVersion(this.program, client.service);
if (versioning && versioning.getVersions()) {
// @versioned in versioning
codeModelClient.apiVersions = [];
for (const version of versioning.getVersions()) {
const apiVersion = new ApiVersion();
apiVersion.version = version.value;
codeModelClient.apiVersions.push(apiVersion);
}

if (!this.sdkContext.apiVersion || ["all", "latest"].includes(this.sdkContext.apiVersion)) {
this.apiVersion = getDefaultApiVersion(this.sdkContext, client.service);
} else {
Expand All @@ -573,6 +567,13 @@ export class CodeModelBuilder {
throw new Error("Unrecognized api-version: " + this.sdkContext.apiVersion);
}
}

codeModelClient.apiVersions = [];
for (const version of this.getFilteredApiVersions(this.apiVersion, versioning.getVersions())) {
const apiVersion = new ApiVersion();
apiVersion.version = version.value;
codeModelClient.apiVersions.push(apiVersion);
}
}

// server
Expand Down Expand Up @@ -665,6 +666,23 @@ export class CodeModelBuilder {
return clients;
}

/**
* Filter api-versions for "ServiceVersion".
* TODO(xiaofei) pending TCGC design: https://github.com/Azure/typespec-azure/issues/746
*
* @param pinnedApiVersion the api-version to use as filter base
* @param versions api-versions to filter
* @returns filtered api-versions
*/
private getFilteredApiVersions(pinnedApiVersion: Version | undefined, versions: Version[]): Version[] {
if (!pinnedApiVersion) {
return versions;
}
return versions
.slice(0, versions.indexOf(pinnedApiVersion) + 1)
.filter((version) => !isStable(pinnedApiVersion) || isStable(version));
}

/**
* `@armProviderNamespace` currently will add a default server if not defined globally:
* https://github.com/Azure/typespec-azure/blob/8b8d7c05f168d9305a09691c4fedcb88f4a57652/packages/typespec-azure-resource-manager/src/namespace.ts#L121-L128
Expand All @@ -678,9 +696,6 @@ export class CodeModelBuilder {
}

private needToSkipProcessingOperation(operation: Operation, clientContext: ClientContext): boolean {
if (!this.existsAtCurrentVersion(operation)) {
return true;
}
// don't generate protocol and convenience method for overloaded operations
// issue link: https://github.com/Azure/autorest.java/issues/1958#issuecomment-1562558219 we will support generate overload methods for non-union type in future (TODO issue: https://github.com/Azure/autorest.java/issues/2160)
if (getOverloadedOperation(this.program, operation)) {
Expand All @@ -690,20 +705,6 @@ export class CodeModelBuilder {
return false;
}

private existsAtCurrentVersion(type: Type): boolean {
const availabilityMap = getAvailabilityMap(this.program, type);
// if unversioned then everything exists
if (
!availabilityMap ||
!this.apiVersion ||
this.supportsAdvancedVersioning() // if supports non-breaking versioning, then it always exists
) {
return true;
}
const availability = availabilityMap.get(this.apiVersion?.name);
return availability === Availability.Added || availability === Availability.Available;
}

/**
* Whether we support advanced versioning in non-breaking fashion.
*/
Expand Down Expand Up @@ -800,9 +801,7 @@ export class CodeModelBuilder {
// host
clientContext.hostParameters.forEach((it) => codeModelOperation.addParameter(it));
// parameters
op.parameters.parameters
.filter((param) => this.existsAtCurrentVersion(param.param))
.map((it) => this.processParameter(codeModelOperation, it, clientContext));
op.parameters.parameters.map((it) => this.processParameter(codeModelOperation, it, clientContext));
// "accept" header
this.addAcceptHeaderParameter(codeModelOperation, op.responses);
// body
Expand Down Expand Up @@ -2294,8 +2293,7 @@ export class CodeModelBuilder {
if (
prop.name === discriminatorPropertyName || // skip the discriminator property
isNeverType(prop.type) || // skip property of type "never"
!isPayloadProperty(this.program, prop) ||
!this.existsAtCurrentVersion(prop)
!isPayloadProperty(this.program, prop)
) {
continue;
}
Expand Down Expand Up @@ -2331,9 +2329,7 @@ export class CodeModelBuilder {
const resource = this.dummyObjectSchema(type, resourceModelName, namespace);
const declaredProperties = walkPropertiesInherited(type);
for (const prop of declaredProperties) {
if (this.existsAtCurrentVersion(type)) {
resource.addProperty(this.processModelProperty(prop));
}
resource.addProperty(this.processModelProperty(prop));
}
return resource;
}
Expand Down
5 changes: 5 additions & 0 deletions typespec-extension/src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { SchemaContext } from "@autorest/codemodel";
import { DurationSchema } from "./common/schemas/time.js";
import { getNamespace, pascalCase } from "./utils.js";
import { getUnionAsEnum } from "@azure-tools/typespec-azure-core";
import { Version } from "@typespec/versioning";

/** Acts as a cache for processing inputs.
*
Expand All @@ -51,6 +52,10 @@ export class ProcessingCache<In, Out> {
}
}

export function isStable(version: Version): boolean {
return !version.value.toLowerCase().includes("preview");
}

/** adds only if the item is not in the collection already
*
* @note While this isn't very efficient, it doesn't disturb the original
Expand Down
2 changes: 1 addition & 1 deletion typespec-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@azure-tools/cadl-ranch-specs": "0.32.0",
"@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.15.13.tgz"
"@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.15.14.tgz"
},
"devDependencies": {
"@typespec/prettier-plugin-typespec": "~0.55.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,10 @@
* Service version of VersioningClient.
*/
public enum VersioningServiceVersion implements ServiceVersion {
/**
* Enum value 2022-06-01-preview.
*/
V2022_06_01_PREVIEW("2022-06-01-preview"),

/**
* Enum value 2022-09-01.
*/
V2022_09_01("2022-09-01"),

/**
* Enum value 2022-12-01-preview.
*/
V2022_12_01_PREVIEW("2022-12-01-preview");
V2022_09_01("2022-09-01");

private final String version;

Expand All @@ -45,6 +35,6 @@ public String getVersion() {
* @return The latest {@link VersioningServiceVersion}.
*/
public static VersioningServiceVersion getLatest() {
return V2022_12_01_PREVIEW;
return V2022_09_01;
}
}

0 comments on commit 5a6afa3

Please sign in to comment.