Skip to content

Commit c7bb95c

Browse files
Copilotmsyyctadelesh
authored
Add test cases for @clientDefaultValue decorator (#3635)
- [x] Create new directory for client-default-value test cases - [x] Add test case 1: @clientDefaultValue for model property - [x] Add test case 2: @clientDefaultValue for operation parameter - [x] Add test case 3: @clientDefaultValue for first path segment in multi-segment path - [x] Add main.tsp file with all test scenarios - [x] Add mockapi.ts file with mock API definitions - [x] Fix mockapi.ts to use correct format (query instead of params, json() helper, correct scenario names) - [x] Fix suppress directive placement to be directly above decorators - [x] Make segment1 parameter required (not optional) - [x] Fix changelog location to .chronus/changes/ directory - [x] Add backticks around decorator references in documentation - [x] Update spec-summary.md with client-default-value documentation - [x] Fix Prettier formatting for spec-summary.md - [x] Fix code formatting with Prettier - [x] Run final code review - No issues found - [x] Run security checks - No vulnerabilities found ## Summary Successfully added three comprehensive test cases for the `@clientDefaultValue` decorator: ### Test Case 1: Model Property - Tests `@clientDefaultValue` on model properties - Includes numeric (timeout=30), string (tier="standard"), and boolean (retry=true) default values - Uses PUT operation with request/response body validation ### Test Case 2: Operation Parameter - Tests `@clientDefaultValue` on operation parameters - Includes query parameters with default values (pageSize=10, format="json") - Uses GET operation with query parameter validation ### Test Case 3: Path Parameter with Multiple Segments - Tests `@clientDefaultValue` on the first path segment in a 2-segment path - Path structure: `/path-parameter/{segment1}/{segment2}` - First segment has default value "default-segment1" and is required - Second segment is required without default All test cases follow existing repository patterns and conventions. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>[spector] add new case for decorator `@clientDefaultValue`</issue_title> > <issue_description>Context: > > `@clientDefaultValue` definition is here https://github.com/Azure/typespec-azure/blob/32292dabd9a4741b02a211fe3838a7279961f690/website/src/content/docs/docs/libraries/typespec-client-generator-core/reference/decorators.md?plain=1#L1154-L1168 > > Goal: > Add test case in folder https://github.com/Azure/typespec-azure/tree/main/packages/azure-http-specs/specs/azure/client-generator-core > > Ask: > - Add 3 cases: > 1. for model property > 2. for operation parameter > 3. this case has 2 path segments and add client default value for first path segment.</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes #3633 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: msyyc <[email protected]> Co-authored-by: Yuchao Yan <[email protected]> Co-authored-by: tadelesh <[email protected]>
1 parent 48b8742 commit c7bb95c

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: feature
3+
packages:
4+
- "@azure-tools/azure-http-specs"
5+
---
6+
7+
Add test cases for `@clientDefaultValue` decorator.

packages/azure-http-specs/spec-summary.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,59 @@ client.getStandalone();
316316
client.deleteStandalone();
317317
```
318318

319+
### Azure_ClientGenerator_Core_ClientDefaultValue_getOperationParameter
320+
321+
- Endpoint: `get /azure/client-generator-core/client-default-value/operation-parameter`
322+
323+
Test case 2: `@clientDefaultValue` for operation parameter.
324+
This scenario tests that client default values are correctly applied to operation parameters.
325+
326+
Expected query parameter:
327+
name: "test"
328+
pageSize: 10 (default)
329+
format: "json" (default)
330+
331+
Expected response: 204 No Content
332+
333+
### Azure_ClientGenerator_Core_ClientDefaultValue_getPathParameter
334+
335+
- Endpoint: `get /azure/client-generator-core/client-default-value/path-parameter/{segment1}/{segment2}`
336+
337+
Test case 3: `@clientDefaultValue` for first path segment.
338+
This scenario has 2 path segments and tests client default value on the first segment.
339+
340+
Expected path parameters:
341+
segment1: "default-segment1" (default)
342+
segment2: "segment2"
343+
344+
Expected response: 204 No Content
345+
346+
### Azure_ClientGenerator_Core_ClientDefaultValue_putModelProperty
347+
348+
- Endpoint: `put /azure/client-generator-core/client-default-value/model-property`
349+
350+
Test case 1: `@clientDefaultValue` for model property.
351+
This scenario tests that client default values are correctly applied to model properties.
352+
353+
Expected input body:
354+
355+
```json
356+
{
357+
"name": "test"
358+
}
359+
```
360+
361+
Expected response body:
362+
363+
```json
364+
{
365+
"name": "test",
366+
"timeout": 30,
367+
"tier": "standard",
368+
"retry": true
369+
}
370+
```
371+
319372
### Azure_ClientGenerator_Core_ClientLocation_MoveMethodParameterToClient
320373

321374
- Endpoint: `get /azure/client-generator-core/client-location/blob`
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import "@typespec/http";
2+
import "@typespec/spector";
3+
import "@azure-tools/typespec-client-generator-core";
4+
5+
using Http;
6+
using Azure.ClientGenerator.Core;
7+
using Spector;
8+
9+
@doc("Test for `@clientDefaultValue` decorator.")
10+
@scenarioService("/azure/client-generator-core/client-default-value")
11+
@global.Azure.ClientGenerator.Core.clientNamespace(
12+
"azure.clientgenerator.core.clientdefaultvalue",
13+
"java"
14+
)
15+
@global.Azure.ClientGenerator.Core.clientNamespace(
16+
"specs.azure.clientgenerator.core.clientdefaultvalue",
17+
"python"
18+
)
19+
namespace _Specs_.Azure.ClientGenerator.Core.ClientDefaultValue;
20+
21+
@doc("Model with client default values on properties.")
22+
model ModelWithDefaultValues {
23+
@doc("Name property with no default value.")
24+
name: string;
25+
26+
#suppress "@azure-tools/typespec-azure-core/no-legacy-usage" "Testing legacy decorator"
27+
@doc("Timeout property with client default value of 30.")
28+
@global.Azure.ClientGenerator.Core.Legacy.clientDefaultValue(30)
29+
timeout?: int32;
30+
31+
#suppress "@azure-tools/typespec-azure-core/no-legacy-usage" "Testing legacy decorator"
32+
@doc("Tier property with client default value of 'standard'.")
33+
@global.Azure.ClientGenerator.Core.Legacy.clientDefaultValue("standard")
34+
tier?: string;
35+
36+
#suppress "@azure-tools/typespec-azure-core/no-legacy-usage" "Testing legacy decorator"
37+
@doc("Retry property with client default value of true.")
38+
@global.Azure.ClientGenerator.Core.Legacy.clientDefaultValue(true)
39+
retry?: boolean;
40+
}
41+
42+
@scenario
43+
@route("/model-property")
44+
@scenarioDoc("""
45+
Test case 1: `@clientDefaultValue` for model property.
46+
This scenario tests that client default values are correctly applied to model properties.
47+
48+
Expected input body:
49+
```json
50+
{
51+
"name": "test"
52+
}
53+
```
54+
55+
Expected response body:
56+
```json
57+
{
58+
"name": "test",
59+
"timeout": 30,
60+
"tier": "standard",
61+
"retry": true
62+
}
63+
```
64+
""")
65+
@put
66+
op putModelProperty(@body body: ModelWithDefaultValues): ModelWithDefaultValues;
67+
68+
@scenario
69+
@route("/operation-parameter")
70+
@scenarioDoc("""
71+
Test case 2: `@clientDefaultValue` for operation parameter.
72+
This scenario tests that client default values are correctly applied to operation parameters.
73+
74+
Expected query parameter:
75+
name: "test"
76+
pageSize: 10 (default)
77+
format: "json" (default)
78+
79+
Expected response: 204 No Content
80+
""")
81+
@get
82+
op getOperationParameter(
83+
@query name: string,
84+
85+
#suppress "@azure-tools/typespec-azure-core/no-legacy-usage" "Testing legacy decorator"
86+
@global.Azure.ClientGenerator.Core.Legacy.clientDefaultValue(10)
87+
@query
88+
pageSize?: int32,
89+
90+
#suppress "@azure-tools/typespec-azure-core/no-legacy-usage" "Testing legacy decorator"
91+
@global.Azure.ClientGenerator.Core.Legacy.clientDefaultValue("json")
92+
@query
93+
format?: string,
94+
): void;
95+
96+
@scenario
97+
@route("/path-parameter/{segment1}/{segment2}")
98+
@scenarioDoc("""
99+
Test case 3: `@clientDefaultValue` for first path segment.
100+
This scenario has 2 path segments and tests client default value on the first segment.
101+
102+
Expected path parameters:
103+
segment1: "default-segment1" (default)
104+
segment2: "segment2"
105+
106+
Expected response: 204 No Content
107+
""")
108+
@get
109+
op getPathParameter(
110+
#suppress "@azure-tools/typespec-azure-core/no-legacy-usage" "Testing legacy decorator"
111+
@global.Azure.ClientGenerator.Core.Legacy.clientDefaultValue("default-segment1")
112+
@path
113+
segment1: string,
114+
115+
@path segment2: string,
116+
): void;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { json, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";
2+
3+
export const Scenarios: Record<string, ScenarioMockApi> = {};
4+
5+
Scenarios.Azure_ClientGenerator_Core_ClientDefaultValue_putModelProperty = passOnSuccess([
6+
{
7+
uri: "/azure/client-generator-core/client-default-value/model-property",
8+
method: "put",
9+
request: {
10+
body: json({
11+
name: "test",
12+
}),
13+
},
14+
response: {
15+
status: 200,
16+
body: json({
17+
name: "test",
18+
timeout: 30,
19+
tier: "standard",
20+
retry: true,
21+
}),
22+
},
23+
kind: "MockApiDefinition",
24+
},
25+
]);
26+
27+
Scenarios.Azure_ClientGenerator_Core_ClientDefaultValue_getOperationParameter = passOnSuccess([
28+
{
29+
uri: "/azure/client-generator-core/client-default-value/operation-parameter",
30+
method: "get",
31+
request: {
32+
query: {
33+
name: "test",
34+
pageSize: 10,
35+
format: "json",
36+
},
37+
},
38+
response: {
39+
status: 204,
40+
},
41+
kind: "MockApiDefinition",
42+
},
43+
]);
44+
45+
Scenarios.Azure_ClientGenerator_Core_ClientDefaultValue_getPathParameter = passOnSuccess([
46+
{
47+
uri: "/azure/client-generator-core/client-default-value/path-parameter/default-segment1/segment2",
48+
method: "get",
49+
request: {},
50+
response: {
51+
status: 204,
52+
},
53+
kind: "MockApiDefinition",
54+
},
55+
]);

0 commit comments

Comments
 (0)