Skip to content

Commit

Permalink
specs, add test for encode integer as string (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Aug 14, 2024
1 parent bdf1466 commit cdb4a5f
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-trainers-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@azure-tools/cadl-ranch-specs": patch
---

Add test case for encode integer as string.
42 changes: 42 additions & 0 deletions packages/cadl-ranch-specs/cadl-ranch-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,48 @@ Expected query parameter `input=36,47`
Test iso8601 encode for a duration parameter.
Expected query parameter `input=P40D`

### Encode_Numeric_Property_safeintAsString

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

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"
}
```

### Encode_Numeric_Property_uint32AsStringOptional

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

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"
}
```

### Parameters_Basic_ExplicitBody_simple

- Endpoint: `put /parameters/basic/explicit-body/simple`
Expand Down
65 changes: 65 additions & 0 deletions packages/cadl-ranch-specs/http/encode/numeric/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import "@typespec/http";
import "@azure-tools/cadl-ranch-expect";
import "@azure-tools/typespec-client-generator-core";

using TypeSpec.Http;
using Azure.ClientGenerator.Core;

@doc("Test for encode decorator on integer.")
@supportedBy("dpg")
@scenarioService("/encode/numeric")
namespace Encode.Numeric;

@operationGroup
@route("/property")
namespace Property {
@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;

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

@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;

model Uint32AsStringProperty {
@encode(string)
value?: uint32;
}
}
19 changes: 19 additions & 0 deletions packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { passOnSuccess, mockapi, json, MockApi } from "@azure-tools/cadl-ranch-api";
import { ScenarioMockApi } from "@azure-tools/cadl-ranch-api";

export const Scenarios: Record<string, ScenarioMockApi> = {};

function createPropertyMockApis(route: string, value: string): MockApi {
const url = `/encode/numeric/property/${route}`;
return mockapi.post(url, (req) => {
req.expect.coercedBodyEquals({ value: value });
return {
status: 200,
body: json({ value: value }),
};
});
}

Scenarios.Encode_Numeric_Property_safeintAsString = passOnSuccess(createPropertyMockApis("safeint", "10000000000"));

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

0 comments on commit cdb4a5f

Please sign in to comment.