Skip to content

Commit 5a6afa3

Browse files
dpg, make ServiceVersion only contain api-versions no later than the pinned one (#2724)
* add ServiceVersion filter * re-generate test code * format * version sort * bump version --------- Co-authored-by: actions-user <[email protected]>
1 parent 7103f43 commit 5a6afa3

File tree

7 files changed

+46
-49
lines changed

7 files changed

+46
-49
lines changed

typespec-extension/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release History
22

3+
## 0.15.14 (2024-04-27)
4+
5+
Compatible with compiler 0.55.
6+
7+
- Added `ServiceVersion` filter for pinned api-version.
8+
39
## 0.15.13 (2024-04-26)
410

511
Compatible with compiler 0.55.

typespec-extension/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typespec-extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure-tools/typespec-java",
3-
"version": "0.15.13",
3+
"version": "0.15.14",
44
"description": "TypeSpec library for emitting Java client from the TypeSpec REST protocol binding",
55
"keywords": [
66
"TypeSpec"

typespec-extension/src/code-model-builder.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import {
5858
isPathParam,
5959
HttpOperationBody,
6060
} from "@typespec/http";
61-
import { Availability, Version, getAddedOnVersions, getAvailabilityMap, getVersion } from "@typespec/versioning";
61+
import { Version, getAddedOnVersions, getVersion } from "@typespec/versioning";
6262
import {
6363
isPollingLocation,
6464
getPagedResult,
@@ -164,6 +164,7 @@ import {
164164
modelIs,
165165
getNamePrefixForProperty,
166166
isAllValueInteger,
167+
isStable,
167168
} from "./type-utils.js";
168169
import {
169170
getServiceVersion,
@@ -558,13 +559,6 @@ export class CodeModelBuilder {
558559
const versioning = getVersion(this.program, client.service);
559560
if (versioning && versioning.getVersions()) {
560561
// @versioned in versioning
561-
codeModelClient.apiVersions = [];
562-
for (const version of versioning.getVersions()) {
563-
const apiVersion = new ApiVersion();
564-
apiVersion.version = version.value;
565-
codeModelClient.apiVersions.push(apiVersion);
566-
}
567-
568562
if (!this.sdkContext.apiVersion || ["all", "latest"].includes(this.sdkContext.apiVersion)) {
569563
this.apiVersion = getDefaultApiVersion(this.sdkContext, client.service);
570564
} else {
@@ -573,6 +567,13 @@ export class CodeModelBuilder {
573567
throw new Error("Unrecognized api-version: " + this.sdkContext.apiVersion);
574568
}
575569
}
570+
571+
codeModelClient.apiVersions = [];
572+
for (const version of this.getFilteredApiVersions(this.apiVersion, versioning.getVersions())) {
573+
const apiVersion = new ApiVersion();
574+
apiVersion.version = version.value;
575+
codeModelClient.apiVersions.push(apiVersion);
576+
}
576577
}
577578

578579
// server
@@ -665,6 +666,23 @@ export class CodeModelBuilder {
665666
return clients;
666667
}
667668

669+
/**
670+
* Filter api-versions for "ServiceVersion".
671+
* TODO(xiaofei) pending TCGC design: https://github.com/Azure/typespec-azure/issues/746
672+
*
673+
* @param pinnedApiVersion the api-version to use as filter base
674+
* @param versions api-versions to filter
675+
* @returns filtered api-versions
676+
*/
677+
private getFilteredApiVersions(pinnedApiVersion: Version | undefined, versions: Version[]): Version[] {
678+
if (!pinnedApiVersion) {
679+
return versions;
680+
}
681+
return versions
682+
.slice(0, versions.indexOf(pinnedApiVersion) + 1)
683+
.filter((version) => !isStable(pinnedApiVersion) || isStable(version));
684+
}
685+
668686
/**
669687
* `@armProviderNamespace` currently will add a default server if not defined globally:
670688
* https://github.com/Azure/typespec-azure/blob/8b8d7c05f168d9305a09691c4fedcb88f4a57652/packages/typespec-azure-resource-manager/src/namespace.ts#L121-L128
@@ -678,9 +696,6 @@ export class CodeModelBuilder {
678696
}
679697

680698
private needToSkipProcessingOperation(operation: Operation, clientContext: ClientContext): boolean {
681-
if (!this.existsAtCurrentVersion(operation)) {
682-
return true;
683-
}
684699
// don't generate protocol and convenience method for overloaded operations
685700
// 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)
686701
if (getOverloadedOperation(this.program, operation)) {
@@ -690,20 +705,6 @@ export class CodeModelBuilder {
690705
return false;
691706
}
692707

693-
private existsAtCurrentVersion(type: Type): boolean {
694-
const availabilityMap = getAvailabilityMap(this.program, type);
695-
// if unversioned then everything exists
696-
if (
697-
!availabilityMap ||
698-
!this.apiVersion ||
699-
this.supportsAdvancedVersioning() // if supports non-breaking versioning, then it always exists
700-
) {
701-
return true;
702-
}
703-
const availability = availabilityMap.get(this.apiVersion?.name);
704-
return availability === Availability.Added || availability === Availability.Available;
705-
}
706-
707708
/**
708709
* Whether we support advanced versioning in non-breaking fashion.
709710
*/
@@ -800,9 +801,7 @@ export class CodeModelBuilder {
800801
// host
801802
clientContext.hostParameters.forEach((it) => codeModelOperation.addParameter(it));
802803
// parameters
803-
op.parameters.parameters
804-
.filter((param) => this.existsAtCurrentVersion(param.param))
805-
.map((it) => this.processParameter(codeModelOperation, it, clientContext));
804+
op.parameters.parameters.map((it) => this.processParameter(codeModelOperation, it, clientContext));
806805
// "accept" header
807806
this.addAcceptHeaderParameter(codeModelOperation, op.responses);
808807
// body
@@ -2294,8 +2293,7 @@ export class CodeModelBuilder {
22942293
if (
22952294
prop.name === discriminatorPropertyName || // skip the discriminator property
22962295
isNeverType(prop.type) || // skip property of type "never"
2297-
!isPayloadProperty(this.program, prop) ||
2298-
!this.existsAtCurrentVersion(prop)
2296+
!isPayloadProperty(this.program, prop)
22992297
) {
23002298
continue;
23012299
}
@@ -2331,9 +2329,7 @@ export class CodeModelBuilder {
23312329
const resource = this.dummyObjectSchema(type, resourceModelName, namespace);
23322330
const declaredProperties = walkPropertiesInherited(type);
23332331
for (const prop of declaredProperties) {
2334-
if (this.existsAtCurrentVersion(type)) {
2335-
resource.addProperty(this.processModelProperty(prop));
2336-
}
2332+
resource.addProperty(this.processModelProperty(prop));
23372333
}
23382334
return resource;
23392335
}

typespec-extension/src/type-utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { SchemaContext } from "@autorest/codemodel";
2525
import { DurationSchema } from "./common/schemas/time.js";
2626
import { getNamespace, pascalCase } from "./utils.js";
2727
import { getUnionAsEnum } from "@azure-tools/typespec-azure-core";
28+
import { Version } from "@typespec/versioning";
2829

2930
/** Acts as a cache for processing inputs.
3031
*
@@ -51,6 +52,10 @@ export class ProcessingCache<In, Out> {
5152
}
5253
}
5354

55+
export function isStable(version: Version): boolean {
56+
return !version.value.toLowerCase().includes("preview");
57+
}
58+
5459
/** adds only if the item is not in the collection already
5560
*
5661
* @note While this isn't very efficient, it doesn't disturb the original

typespec-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@azure-tools/cadl-ranch-specs": "0.32.0",
13-
"@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.15.13.tgz"
13+
"@azure-tools/typespec-java": "file:/../typespec-extension/azure-tools-typespec-java-0.15.14.tgz"
1414
},
1515
"devDependencies": {
1616
"@typespec/prettier-plugin-typespec": "~0.55.0",

typespec-tests/src/main/java/com/cadl/versioning/VersioningServiceVersion.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,10 @@
1010
* Service version of VersioningClient.
1111
*/
1212
public enum VersioningServiceVersion implements ServiceVersion {
13-
/**
14-
* Enum value 2022-06-01-preview.
15-
*/
16-
V2022_06_01_PREVIEW("2022-06-01-preview"),
17-
1813
/**
1914
* Enum value 2022-09-01.
2015
*/
21-
V2022_09_01("2022-09-01"),
22-
23-
/**
24-
* Enum value 2022-12-01-preview.
25-
*/
26-
V2022_12_01_PREVIEW("2022-12-01-preview");
16+
V2022_09_01("2022-09-01");
2717

2818
private final String version;
2919

@@ -45,6 +35,6 @@ public String getVersion() {
4535
* @return The latest {@link VersioningServiceVersion}.
4636
*/
4737
public static VersioningServiceVersion getLatest() {
48-
return V2022_12_01_PREVIEW;
38+
return V2022_09_01;
4939
}
5040
}

0 commit comments

Comments
 (0)