Skip to content

Commit

Permalink
refactor(spec): add uint8 for int encoding and templatize (Azure#711)
Browse files Browse the repository at this point in the history
- .net generator doesn't support `uint32` yet, so add a `uint8` test case
- templatize int encoding test cases

---------

Co-authored-by: Mingzhe Huang (from Dev Box) <[email protected]>
  • Loading branch information
2 people authored and v-hongli1 committed Aug 29, 2024
1 parent 4e639a8 commit cddc6c6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-walls-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@azure-tools/cadl-ranch-specs": patch
---

refactor(spec): add uint8 for int encoding and templatize
21 changes: 21 additions & 0 deletions packages/cadl-ranch-specs/cadl-ranch-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,27 @@ Expected response body:
}
```

### Encode_Numeric_Property_uint8AsString

- Endpoint: `post /encode/numeric/property/uint8`

Test operation with request and response model contains property of uint8 type with string encode.
Expected request body:

```json
{
"value": "255"
}
```

Expected response body:

```json
{
"value": "255"
}
```

### Parameters_Basic_ExplicitBody_simple

- Endpoint: `put /parameters/basic/explicit-body/simple`
Expand Down
79 changes: 43 additions & 36 deletions packages/cadl-ranch-specs/http/encode/numeric/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,60 @@ namespace Encode.Numeric;
@operationGroup
@route("/property")
namespace Property {
alias SendSafeIntAsString = SendIntAsString<safeint, "10000000000", SafeintAsStringProperty>;

@route("/safeint")
@scenario
@scenarioDoc("""
Test operation with request and response model contains property of safeint type with string encode.
Expected request body:
```json
{
"value": "10000000000"
}
```
Expected response body:
```json
{
"value": "10000000000"
}
```
""")
@post
op safeintAsString(@body body: SafeintAsStringProperty): SafeintAsStringProperty;
op safeintAsString is SendSafeIntAsString.sendIntAsString;

model SafeintAsStringProperty {
@encode(string)
value: safeint;
}

alias SendUint32AsString = SendIntAsString<uint32, "1", Uint32AsStringProperty>;

@route("/uint32")
@scenario
@scenarioDoc("""
Test operation with request and response model contains property of uint32 type with string encode.
Expected request body:
```json
{
"value": "1"
}
```
Expected response body:
```json
{
"value": "1"
}
```
""")
@post
op uint32AsStringOptional(@body body: Uint32AsStringProperty): Uint32AsStringProperty;
op uint32AsStringOptional is SendUint32AsString.sendIntAsString;

model Uint32AsStringProperty {
@encode(string)
value?: uint32;
}

alias SendUint8AsString = SendIntAsString<uint8, "255", Uint8AsStringProperty>;

@route("/uint8")
op uint8AsString is SendUint8AsString.sendIntAsString;

model Uint8AsStringProperty {
@encode(string)
value: uint8;
}

interface SendIntAsString<IntType extends integer, StringValue extends string, Payload> {
@scenario
@scenarioDoc(
"""
Test operation with request and response model contains property of {type} type with string encode.
Expected request body:
```json
{
"value": "{value}"
}
```
Expected response body:
```json
{
"value": "{value}"
}
```
""",
{
type: IntType,
value: StringValue,
}
)
@post
sendIntAsString(@body value: Payload): Payload;
}
}
2 changes: 2 additions & 0 deletions packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ function createPropertyMockApis(route: string, value: string): MockApi {
Scenarios.Encode_Numeric_Property_safeintAsString = passOnSuccess(createPropertyMockApis("safeint", "10000000000"));

Scenarios.Encode_Numeric_Property_uint32AsStringOptional = passOnSuccess(createPropertyMockApis("uint32", "1"));

Scenarios.Encode_Numeric_Property_uint8AsString = passOnSuccess(createPropertyMockApis("uint8", "255"));

0 comments on commit cddc6c6

Please sign in to comment.