Commit 2de1cbe
.Net: [RestApi] Dynamic payload generation with namespaced payload parameters (#2467)
### Motivation and Context
The dynamic payload creation was requested by a few internal teams. It
allows SK RestAPI functionality consumer code to only supply arguments
for parameters specified in the OpenAPI payload metadata for PUT and
POST operations, while the rest – payload constriction, data type
conversion, parameter namespacing, etc. – is handled by SK RestAPI
functionality.
### Description
#### Context
Today, the only way to pass a payload for a PUT or POST RestAPI
operation is by adding the `payload` argument to the context variables
collection before executing the operation. The `payload` argument, for
operations requiring an 'application/json' payload, is expected to be a
valid JSON with all the necessary properties initialized. This approach
is not convenient for SK consumers who only want to provide the required
payload parameters, similar to how they handle query string and header
parameters, without having to construct the payload themselves.
#### Dynamic creation of payload
By default, dynamic payload creation is disabled to ensure backward
compatibility. To enable it, the `BuildOperationPayloadDynamically`
property of the `OpenApiSkillExecutionParameters` execution parameters
should be set to `true` when importing the AI plugin:
```csharp
var plugin = await kernel.ImportAIPluginAsync("<skill name>", new Uri("<chatGPT-plugin>"), new OpenApiSkillExecutionParameters(httpClient) { BuildOperationPayloadDynamically = true });
```
So, if a RestAPI operation payload schema requires payload like the
following:
```json
{
"value": "secret-value",
"attributes": {
"enabled": true
}
}
```
To dynamically build it, the consumer code needs to register the
following arguments in the context variables collection:
```csharp
var contextVariables = new ContextVariables();
contextVariables.Set("value", "secret-value");
contextVariables.Set("enabled", true);
```
#### Payload parameter namespacing
When building payload dynamically, and a RestAPI operation requires the
'application/json' content type, it's possible that the content may have
properties with the identical names, see example below, at different
levels. In such cases, SK can't resolve their values unambiguously from
a flat list of arguments unless there's a namespacing mechanism that
allows the distinction of those properties from one another.
The namespacing mechanism relies on prefixing parameter names with their
parent parameter name, separated by dots. So, the 'namespaced' parameter
names should be used when adding arguments to the context variables
collection. For example, consider this JSON:
```json
{
"upn": "<sender upn>",
"receiver": {
"upn": "<receiver upn>"
},
"cc": {
"upn": "<cc upn>"
}
}
```
It contains `upn` properties at different levels. The argument
registration for the parameters (property values) will look like:
```csharp
var contextVariables = new ContextVariables();
contextVariables.Set("upn", "<sender-upn-value>");
contextVariables.Set("receiver.upn", "<receiver-upn-value>");
contextVariables.Set("cc.upn", "<cc-upn-value>");
```
The namespacing mechanism is disabled by default for backward
compatibility and can be easily enabled by setting the value `true` to
the `NamespacePayloadParameters` property of the
`OpenApiSkillExecutionParameters` execution parameters when importing
the AI plugin:
```csharp
var plugin = await kernel.ImportAIPluginAsync("<skill name>", new Uri("<chatGPT-plugin>"), new OpenApiSkillExecutionParameters(httpClient) { NamespacePayloadParameters = true });
```
#### Parameter arguments data type conversion
Currently, the SK context variables collection only supports string
variables.This means that in order to set or add a primitive or complex
type variable, it must first be converted or serialized into a string.
However, this behavior causes issues when the RestAPI functionality
sends HTTP requests with an 'application/json' payload. For instance, a
property like enabled might have the string value `"true"` instead of
the intended Boolean value `true`.
To mitigate this problem, the RestAPIOperationRunner class iterates over
the operation payload metadata. During this process, it converts all
string arguments to their corresponding types as specified in the
metadata. This ensures accurate type representation within the operation
payload.
### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
---------
Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>1 parent f87290f commit 2de1cbe
File tree
13 files changed
+1172
-93
lines changed- docs/decisions
- dotnet
- samples/KernelSyntaxExamples
- src/Skills
- Skills.OpenAPI
- Extensions
- OpenApi
- Skills.UnitTests
- Connectors/WebApi/Rest
- OpenAPI
- Extensions
13 files changed
+1172
-93
lines changedLines changed: 79 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
Lines changed: 28 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
| 85 | + | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
| 124 | + | |
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | | - | |
| 133 | + | |
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
| 172 | + | |
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | | - | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
183 | 188 | | |
184 | 189 | | |
185 | 190 | | |
| |||
189 | 194 | | |
190 | 195 | | |
191 | 196 | | |
192 | | - | |
| 197 | + | |
193 | 198 | | |
194 | 199 | | |
195 | 200 | | |
| |||
208 | 213 | | |
209 | 214 | | |
210 | 215 | | |
211 | | - | |
| 216 | + | |
212 | 217 | | |
213 | 218 | | |
214 | 219 | | |
| |||
218 | 223 | | |
219 | 224 | | |
220 | 225 | | |
221 | | - | |
| 226 | + | |
222 | 227 | | |
223 | 228 | | |
224 | 229 | | |
| |||
228 | 233 | | |
229 | 234 | | |
230 | 235 | | |
231 | | - | |
| 236 | + | |
232 | 237 | | |
233 | 238 | | |
234 | 239 | | |
| |||
293 | 298 | | |
294 | 299 | | |
295 | 300 | | |
296 | | - | |
| 301 | + | |
297 | 302 | | |
298 | 303 | | |
299 | 304 | | |
300 | 305 | | |
301 | 306 | | |
302 | 307 | | |
303 | 308 | | |
304 | | - | |
| 309 | + | |
305 | 310 | | |
306 | 311 | | |
307 | | - | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
308 | 317 | | |
309 | 318 | | |
310 | 319 | | |
| |||
Lines changed: 26 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
41 | 56 | | |
42 | 57 | | |
43 | 58 | | |
| |||
49 | 64 | | |
50 | 65 | | |
51 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
52 | 71 | | |
53 | 72 | | |
54 | 73 | | |
55 | 74 | | |
56 | | - | |
57 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
58 | 79 | | |
59 | 80 | | |
60 | 81 | | |
61 | 82 | | |
62 | 83 | | |
63 | 84 | | |
| 85 | + | |
| 86 | + | |
64 | 87 | | |
65 | 88 | | |
0 commit comments