Skip to content

Commit 382fb8c

Browse files
Copilottadeleshtadelesh
authored
[http-specs] Add http-specs tests for CSV encoding on model properties (#9106)
Adds scenario tests for array encoding on model properties, covering all four `ArrayEncoding` variants. ## Changes - **New test folder**: `packages/http-specs/specs/encode/array/` - **Scenarios added**: - `commaDelimited` (CSV) - values joined with `,` - `spaceDelimited` (SSV) - values joined with ` ` - `pipeDelimited` - values joined with `|` - `newlineDelimited` - values joined with `\n` ## Example ```tsp model CommaDelimitedArrayProperty { @encode(ArrayEncoding.commaDelimited) value: string[]; } @route("/property/comma-delimited") @post op commaDelimited(@Body body: CommaDelimitedArrayProperty): CommaDelimitedArrayProperty; ``` Expected request/response body: ```json { "value": "blue,red,green" } ``` > [!WARNING] > > <details> > <summary>Firewall rules blocked me from connecting to one or more addresses (expand for details)</summary> > > #### I tried to connect to the following addresses, but was blocked by firewall rules: > > - `telemetry.astro.build` > - Triggering command: `/usr/local/bin/node node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build` (dns block) > > If you need me to access, download, or install something from one of these locations, you can either: > > - Configure [Actions setup steps](https://gh.io/copilot/actions-setup-steps) to set up my environment, which run before the firewall is enabled > - Add the appropriate URLs or hosts to the custom allowlist in this repository's [Copilot coding agent settings](https://github.com/microsoft/typespec/settings/copilot/coding_agent) (admins only) > > </details> <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>CSV encoding for model properties Scenario tests</issue_title> > <issue_description>Add http-specs tests for array encoding, including all four encode. Add a new folder under packages/http-specs/specs/encode.</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes #9029 <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tadelesh <1726438+tadelesh@users.noreply.github.com> Co-authored-by: tadelesh <chenjieshi@microsoft.com>
1 parent a941cb1 commit 382fb8c

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-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+
- "@typespec/http-specs"
5+
---
6+
7+
Add tests for array encode.

packages/http-specs/spec-summary.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,90 @@ Expected behavior: Should handle nested and combined formatting.
105105
Test italic text formatting using _single asterisks_.
106106
Expected behavior: Text between \* should render as italic.
107107

108+
### Encode_Array_Property_commaDelimited
109+
110+
- Endpoint: `post /encode/array/property/comma-delimited`
111+
112+
Test operation with request and response model contains a string array property with commaDelimited encode.
113+
Expected request body:
114+
115+
```json
116+
{
117+
"value": "blue,red,green"
118+
}
119+
```
120+
121+
Expected response body:
122+
123+
```json
124+
{
125+
"value": "blue,red,green"
126+
}
127+
```
128+
129+
### Encode_Array_Property_newlineDelimited
130+
131+
- Endpoint: `post /encode/array/property/newline-delimited`
132+
133+
Test operation with request and response model contains a string array property with newlineDelimited encode.
134+
Expected request body:
135+
136+
```json
137+
{
138+
"value": "blue\nred\ngreen"
139+
}
140+
```
141+
142+
Expected response body:
143+
144+
```json
145+
{
146+
"value": "blue\nred\ngreen"
147+
}
148+
```
149+
150+
### Encode_Array_Property_pipeDelimited
151+
152+
- Endpoint: `post /encode/array/property/pipe-delimited`
153+
154+
Test operation with request and response model contains a string array property with pipeDelimited encode.
155+
Expected request body:
156+
157+
```json
158+
{
159+
"value": "blue|red|green"
160+
}
161+
```
162+
163+
Expected response body:
164+
165+
```json
166+
{
167+
"value": "blue|red|green"
168+
}
169+
```
170+
171+
### Encode_Array_Property_spaceDelimited
172+
173+
- Endpoint: `post /encode/array/property/space-delimited`
174+
175+
Test operation with request and response model contains a string array property with spaceDelimited encode.
176+
Expected request body:
177+
178+
```json
179+
{
180+
"value": "blue red green"
181+
}
182+
```
183+
184+
Expected response body:
185+
186+
```json
187+
{
188+
"value": "blue red green"
189+
}
190+
```
191+
108192
### Encode_Bytes_Header_base64
109193

110194
- Endpoint: `get /encode/bytes/header/base64`
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import "@typespec/http";
2+
import "@typespec/spector";
3+
4+
using Http;
5+
using Spector;
6+
7+
@doc("Test for encode decorator on array.")
8+
@scenarioService("/encode/array")
9+
namespace Encode.Array;
10+
11+
model CommaDelimitedArrayProperty {
12+
@encode(ArrayEncoding.commaDelimited)
13+
value: string[];
14+
}
15+
16+
model SpaceDelimitedArrayProperty {
17+
@encode(ArrayEncoding.spaceDelimited)
18+
value: string[];
19+
}
20+
21+
model PipeDelimitedArrayProperty {
22+
@encode(ArrayEncoding.pipeDelimited)
23+
value: string[];
24+
}
25+
26+
model NewlineDelimitedArrayProperty {
27+
@encode(ArrayEncoding.newlineDelimited)
28+
value: string[];
29+
}
30+
31+
@route("/property")
32+
namespace Property {
33+
@route("/comma-delimited")
34+
@scenario
35+
@scenarioDoc("""
36+
Test operation with request and response model contains a string array property with commaDelimited encode.
37+
Expected request body:
38+
```json
39+
{
40+
"value": "blue,red,green"
41+
}
42+
```
43+
Expected response body:
44+
```json
45+
{
46+
"value": "blue,red,green"
47+
}
48+
```
49+
""")
50+
@post
51+
op commaDelimited(@body body: CommaDelimitedArrayProperty): CommaDelimitedArrayProperty;
52+
53+
@route("/space-delimited")
54+
@scenario
55+
@scenarioDoc("""
56+
Test operation with request and response model contains a string array property with spaceDelimited encode.
57+
Expected request body:
58+
```json
59+
{
60+
"value": "blue red green"
61+
}
62+
```
63+
Expected response body:
64+
```json
65+
{
66+
"value": "blue red green"
67+
}
68+
```
69+
""")
70+
@post
71+
op spaceDelimited(@body body: SpaceDelimitedArrayProperty): SpaceDelimitedArrayProperty;
72+
73+
@route("/pipe-delimited")
74+
@scenario
75+
@scenarioDoc("""
76+
Test operation with request and response model contains a string array property with pipeDelimited encode.
77+
Expected request body:
78+
```json
79+
{
80+
"value": "blue|red|green"
81+
}
82+
```
83+
Expected response body:
84+
```json
85+
{
86+
"value": "blue|red|green"
87+
}
88+
```
89+
""")
90+
@post
91+
op pipeDelimited(@body body: PipeDelimitedArrayProperty): PipeDelimitedArrayProperty;
92+
93+
@route("/newline-delimited")
94+
@scenario
95+
@scenarioDoc("""
96+
Test operation with request and response model contains a string array property with newlineDelimited encode.
97+
Expected request body:
98+
```json
99+
{
100+
"value": "blue\\nred\\ngreen"
101+
}
102+
```
103+
Expected response body:
104+
```json
105+
{
106+
"value": "blue\\nred\\ngreen"
107+
}
108+
```
109+
""")
110+
@post
111+
op newlineDelimited(@body body: NewlineDelimitedArrayProperty): NewlineDelimitedArrayProperty;
112+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { json, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api";
2+
3+
export const Scenarios: Record<string, ScenarioMockApi> = {};
4+
5+
const colors = ["blue", "red", "green"];
6+
7+
function createPropertyServerTests(uri: string, delimiter: string) {
8+
const encodedValue = colors.join(delimiter);
9+
return passOnSuccess({
10+
uri,
11+
method: "post",
12+
request: {
13+
body: json({
14+
value: encodedValue,
15+
}),
16+
},
17+
response: {
18+
status: 200,
19+
body: json({ value: encodedValue }),
20+
},
21+
kind: "MockApiDefinition",
22+
});
23+
}
24+
25+
Scenarios.Encode_Array_Property_commaDelimited = createPropertyServerTests(
26+
"/encode/array/property/comma-delimited",
27+
",",
28+
);
29+
30+
Scenarios.Encode_Array_Property_spaceDelimited = createPropertyServerTests(
31+
"/encode/array/property/space-delimited",
32+
" ",
33+
);
34+
35+
Scenarios.Encode_Array_Property_pipeDelimited = createPropertyServerTests(
36+
"/encode/array/property/pipe-delimited",
37+
"|",
38+
);
39+
40+
Scenarios.Encode_Array_Property_newlineDelimited = createPropertyServerTests(
41+
"/encode/array/property/newline-delimited",
42+
"\n",
43+
);

0 commit comments

Comments
 (0)