From 292f47970b878f5e5abc1ade9ce51a3026c60a40 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Fri, 23 Aug 2024 14:40:51 +0800 Subject: [PATCH 1/6] refactor(spec): replace uint32 with uint8 for int encoding - .net doesn't support uint32 yet, so use uint8 - use a value which will be negative in signed int --- .changeset/lemon-walls-attend.md | 5 +++++ packages/cadl-ranch-specs/cadl-ranch-summary.md | 10 +++++----- .../cadl-ranch-specs/http/encode/numeric/main.tsp | 14 +++++++------- .../http/encode/numeric/mockapi.ts | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 .changeset/lemon-walls-attend.md diff --git a/.changeset/lemon-walls-attend.md b/.changeset/lemon-walls-attend.md new file mode 100644 index 000000000..babb35094 --- /dev/null +++ b/.changeset/lemon-walls-attend.md @@ -0,0 +1,5 @@ +--- +"@azure-tools/cadl-ranch-specs": patch +--- + +refactor(spec): replace uint32 with uint8 for int encoding diff --git a/packages/cadl-ranch-specs/cadl-ranch-summary.md b/packages/cadl-ranch-specs/cadl-ranch-summary.md index 3c31c65dd..dc8576155 100644 --- a/packages/cadl-ranch-specs/cadl-ranch-summary.md +++ b/packages/cadl-ranch-specs/cadl-ranch-summary.md @@ -2246,16 +2246,16 @@ Expected response body: } ``` -### Encode_Numeric_Property_uint32AsStringOptional +### Encode_Numeric_Property_uint8AsStringOptional -- Endpoint: `post /encode/numeric/property/uint32` +- Endpoint: `post /encode/numeric/property/uint8` -Test operation with request and response model contains property of uint32 type with string encode. +Test operation with request and response model contains property of uint8 type with string encode. Expected request body: ```json { - "value": "1" + "value": "255" } ``` @@ -2263,7 +2263,7 @@ Expected response body: ```json { - "value": "1" + "value": "255" } ``` diff --git a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp index 58b2bfd28..d4eaf2c54 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp +++ b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp @@ -38,28 +38,28 @@ namespace Property { value: safeint; } - @route("/uint32") + @route("/uint8") @scenario @scenarioDoc(""" - Test operation with request and response model contains property of uint32 type with string encode. + Test operation with request and response model contains property of uint8 type with string encode. Expected request body: ```json { - "value": "1" + "value": "255" } ``` Expected response body: ```json { - "value": "1" + "value": "255" } ``` """) @post - op uint32AsStringOptional(@body body: Uint32AsStringProperty): Uint32AsStringProperty; + op uint8AsStringOptional(@body body: Uint8AsStringProperty): Uint8AsStringProperty; - model Uint32AsStringProperty { + model Uint8AsStringProperty { @encode(string) - value?: uint32; + value?: uint8; } } diff --git a/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts b/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts index 8785c3818..add13ce14 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts +++ b/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts @@ -16,4 +16,4 @@ 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_uint8AsStringOptional = passOnSuccess(createPropertyMockApis("uint8", "255")); From 5e04d97650ff1b5e69e259ad1d2cd98b54e9cf86 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Mon, 26 Aug 2024 15:23:56 +0800 Subject: [PATCH 2/6] add back uint32 test case --- .../cadl-ranch-specs/cadl-ranch-summary.md | 21 ++++++++++++++++ .../http/encode/numeric/main.tsp | 25 +++++++++++++++++++ .../http/encode/numeric/mockapi.ts | 2 ++ 3 files changed, 48 insertions(+) diff --git a/packages/cadl-ranch-specs/cadl-ranch-summary.md b/packages/cadl-ranch-specs/cadl-ranch-summary.md index dc8576155..27bb39eae 100644 --- a/packages/cadl-ranch-specs/cadl-ranch-summary.md +++ b/packages/cadl-ranch-specs/cadl-ranch-summary.md @@ -2246,6 +2246,27 @@ Expected response body: } ``` +### 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" +} +``` + ### Encode_Numeric_Property_uint8AsStringOptional - Endpoint: `post /encode/numeric/property/uint8` diff --git a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp index d4eaf2c54..cd2c926e1 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp +++ b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp @@ -38,6 +38,31 @@ namespace Property { 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; + } + @route("/uint8") @scenario @scenarioDoc(""" diff --git a/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts b/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts index add13ce14..6983782b1 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts +++ b/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts @@ -16,4 +16,6 @@ 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_uint8AsStringOptional = passOnSuccess(createPropertyMockApis("uint8", "255")); From bdcbdfd65db3836dba896e9313219a636a6619f6 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Tue, 27 Aug 2024 15:09:26 +0800 Subject: [PATCH 3/6] templatize tests --- .../http/encode/numeric/main.tsp | 90 ++++++++----------- 1 file changed, 36 insertions(+), 54 deletions(-) diff --git a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp index cd2c926e1..31328e535 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp +++ b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp @@ -13,78 +13,60 @@ namespace Encode.Numeric; @operationGroup @route("/property") namespace Property { + alias SendSafeIntAsString = SendIntAsString; + @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; + @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; + @route("/uint8") - @scenario - @scenarioDoc(""" - 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" - } - ``` - """) - @post - op uint8AsStringOptional(@body body: Uint8AsStringProperty): Uint8AsStringProperty; + op uint8AsStringOptional is SendUint8AsString.sendIntAsString; model Uint8AsStringProperty { @encode(string) value?: uint8; } + + interface SendIntAsString { + @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; + } } From 3c9217daa0931bb20dcda051759bb2e6cc379f01 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Tue, 27 Aug 2024 15:52:10 +0800 Subject: [PATCH 4/6] make uint8 property required --- packages/cadl-ranch-specs/http/encode/numeric/main.tsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp index 31328e535..0b1f645f9 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp +++ b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp @@ -40,7 +40,7 @@ namespace Property { model Uint8AsStringProperty { @encode(string) - value?: uint8; + value: uint8; } interface SendIntAsString { From 9db3918b01e0f74ce6a142b7364f8df6e999f12b Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Tue, 27 Aug 2024 15:54:14 +0800 Subject: [PATCH 5/6] update changeset summary --- .changeset/lemon-walls-attend.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/lemon-walls-attend.md b/.changeset/lemon-walls-attend.md index babb35094..3aade0f1b 100644 --- a/.changeset/lemon-walls-attend.md +++ b/.changeset/lemon-walls-attend.md @@ -2,4 +2,4 @@ "@azure-tools/cadl-ranch-specs": patch --- -refactor(spec): replace uint32 with uint8 for int encoding +refactor(spec): add uint8 for int encoding and templatize From 43c7331a35c0d2a3556d9220d9ed52037f4e2070 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Tue, 27 Aug 2024 16:45:44 +0800 Subject: [PATCH 6/6] update uint8 senario name --- packages/cadl-ranch-specs/cadl-ranch-summary.md | 2 +- packages/cadl-ranch-specs/http/encode/numeric/main.tsp | 2 +- packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cadl-ranch-specs/cadl-ranch-summary.md b/packages/cadl-ranch-specs/cadl-ranch-summary.md index 2c7b003dd..ad7f7112e 100644 --- a/packages/cadl-ranch-specs/cadl-ranch-summary.md +++ b/packages/cadl-ranch-specs/cadl-ranch-summary.md @@ -2413,7 +2413,7 @@ Expected response body: } ``` -### Encode_Numeric_Property_uint8AsStringOptional +### Encode_Numeric_Property_uint8AsString - Endpoint: `post /encode/numeric/property/uint8` diff --git a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp index 0b1f645f9..413f3345e 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/main.tsp +++ b/packages/cadl-ranch-specs/http/encode/numeric/main.tsp @@ -36,7 +36,7 @@ namespace Property { alias SendUint8AsString = SendIntAsString; @route("/uint8") - op uint8AsStringOptional is SendUint8AsString.sendIntAsString; + op uint8AsString is SendUint8AsString.sendIntAsString; model Uint8AsStringProperty { @encode(string) diff --git a/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts b/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts index 6983782b1..7f6632963 100644 --- a/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts +++ b/packages/cadl-ranch-specs/http/encode/numeric/mockapi.ts @@ -18,4 +18,4 @@ Scenarios.Encode_Numeric_Property_safeintAsString = passOnSuccess(createProperty Scenarios.Encode_Numeric_Property_uint32AsStringOptional = passOnSuccess(createPropertyMockApis("uint32", "1")); -Scenarios.Encode_Numeric_Property_uint8AsStringOptional = passOnSuccess(createPropertyMockApis("uint8", "255")); +Scenarios.Encode_Numeric_Property_uint8AsString = passOnSuccess(createPropertyMockApis("uint8", "255"));