Skip to content

Commit fde8da1

Browse files
authored
Issue found in DPG modular generation - apiVersion parameter missing (#3437)
* add smoke case for @azure-rest/purview-datamap * add ut * add ut * regen code * regen code * fix * regen code * regen code * update typespec * add ut * remove smoke case * update ut * update ut
1 parent 73dc930 commit fde8da1

File tree

2 files changed

+115
-5
lines changed

2 files changed

+115
-5
lines changed

packages/typespec-ts/src/modular/helpers/clientHelpers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ export function getClientParameters(
7373
const hasDefaultValue = (p: SdkParameter) =>
7474
p.clientDefaultValue || p.__raw?.defaultValue || p.type.kind === "constant";
7575
const isRequired = (p: SdkParameter) =>
76-
!p.optional &&
77-
((!hasDefaultValue(p) &&
76+
// Special case: when apiVersionAsRequired is true, apiVersion should always be considered required
77+
(options.apiVersionAsRequired && p.isApiVersionParam) ||
78+
(!p.optional &&
79+
!hasDefaultValue(p) &&
7880
!(
7981
p.type.kind === "endpoint" &&
8082
p.type.templateArguments[0] &&
8183
hasDefaultValue(p.type.templateArguments[0])
82-
)) ||
83-
(options.apiVersionAsRequired && p.isApiVersionParam));
84+
));
8485
const isOptional = (p: SdkParameter) => p.optional || hasDefaultValue(p);
8586
const skipCredentials = (p: SdkParameter) => p.kind !== "credential";
8687
const skipMethodParam = (p: SdkParameter) => p.kind !== "method";
@@ -98,7 +99,6 @@ export function getClientParameters(
9899
const params = clientParams.filter((p) =>
99100
filters.every((filter) => !filter || filter(p))
100101
);
101-
102102
return params;
103103
}
104104

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# handle with optional api-version parameter via custom VersionParameterTrait
2+
3+
## TypeSpec
4+
5+
```tsp
6+
import "@typespec/http";
7+
import "@typespec/rest";
8+
import "@typespec/versioning";
9+
import "@azure-tools/typespec-azure-core";
10+
11+
using TypeSpec.Http;
12+
using TypeSpec.Rest;
13+
using TypeSpec.Versioning;
14+
using Azure.Core;
15+
using Azure.Core.Traits;
16+
17+
@service(#{
18+
title: "DataMapClient",
19+
})
20+
@versioned(DataMapService.Versions)
21+
@server(
22+
"{endpoint}",
23+
"DataMap Service",
24+
{
25+
@doc("Service endpoint")
26+
endpoint: url,
27+
}
28+
)
29+
namespace DataMapService;
30+
31+
enum Versions {
32+
@doc("API Version 2023-09-01")
33+
`2023-09-01`,
34+
}
35+
36+
@route("/entities")
37+
@get
38+
op listEntities(
39+
@doc("The API version to use for this operation.")
40+
@query("api-version")
41+
@minLength(1)
42+
apiVersion?: string,
43+
): {
44+
@statusCode statusCode: 200;
45+
@body entities: string[];
46+
};
47+
```
48+
49+
The config would be like:
50+
51+
```yaml
52+
withRawContent: true
53+
ignoreWeirdLine: false
54+
```
55+
56+
## clientContext
57+
58+
```ts clientContext
59+
import { logger } from "../logger.js";
60+
import { KnownVersions } from "../models/models.js";
61+
import { Client, ClientOptions, getClient } from "@azure-rest/core-client";
62+
63+
export interface DataMapServiceContext extends Client {
64+
/** The API version to use for this operation. */
65+
/** Known values of {@link KnownVersions} that the service accepts. */
66+
apiVersion: string;
67+
}
68+
69+
/** Optional parameters for the client. */
70+
export interface DataMapServiceClientOptionalParams extends ClientOptions {
71+
/** The API version to use for this operation. */
72+
/** Known values of {@link KnownVersions} that the service accepts. */
73+
apiVersion?: string;
74+
}
75+
76+
export function createDataMapService(
77+
endpointParam: string,
78+
options: DataMapServiceClientOptionalParams = {},
79+
): DataMapServiceContext {
80+
const endpointUrl = options.endpoint ?? String(endpointParam);
81+
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
82+
const userAgentPrefix = prefixFromOptions
83+
? `${prefixFromOptions} azsdk-js-api`
84+
: `azsdk-js-api`;
85+
const { apiVersion: _, ...updatedOptions } = {
86+
...options,
87+
userAgentOptions: { userAgentPrefix },
88+
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
89+
};
90+
const clientContext = getClient(endpointUrl, undefined, updatedOptions);
91+
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
92+
const apiVersion = options.apiVersion ?? "2023-09-01";
93+
clientContext.pipeline.addPolicy({
94+
name: "ClientApiVersionPolicy",
95+
sendRequest: (req, next) => {
96+
// Use the apiVersion defined in request url directly
97+
// Append one if there is no apiVersion and we have one at client options
98+
const url = new URL(req.url);
99+
if (!url.searchParams.get("api-version")) {
100+
req.url = `${req.url}${
101+
Array.from(url.searchParams.keys()).length > 0 ? "&" : "?"
102+
}api-version=${apiVersion}`;
103+
}
104+
105+
return next(req);
106+
},
107+
});
108+
return { ...clientContext, apiVersion } as DataMapServiceContext;
109+
}
110+
```

0 commit comments

Comments
 (0)