Commit 0942870
Support serialization of object examples on unions (#5180)
This PR fixes this issue:
* #4869
Currently, the `@typespec/openapi3` emitter supports `@example`s for
`model`s, but outputs incorrect examples for `union`s.
For example ([playground
link](https://typespec.io/playground/?c=QGV4YW1wbGUoI3sgYTogMSB9KQptb2RlbCBBIHsKIMQUaW50ZWdlcjsKfQoK1C91bmlvbiBVxS9udW1iZXI6xQzMPSwKICB9xAV0ZXh0yB9zOiBzdHJpbmfHHn0K&e=%40typespec%2Fopenapi3&options=%7B%7D)):
```typespec
@example(#{ a: 1 })
union U {
number: {
a: integer,
},
text: {
s: string,
},
}
```
should output with `{ a: 1 }` as the example, but instead it is `{}`.
The issue manifests in the `serializeValueAsJson` function in
packages/compiler/src/lib/examples.ts, and occurs because the
`resolveUnions` function in the same file fails to detect the
appropriate union variant.
With this PR, the example is emitted correctly. I've added a unit test
to confirm this.
---------
Co-authored-by: Christopher Radek <[email protected]>1 parent 68228c8 commit 0942870
File tree
3 files changed
+34
-2
lines changed- .chronus/changes
- packages/compiler
- src/lib
- test/decorators
3 files changed
+34
-2
lines changedLines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
79 | | - | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
| 176 | + | |
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
185 | 209 | | |
186 | 210 | | |
187 | 211 | | |
| |||
0 commit comments