You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@get op list(param: string): ListResponse | TypicalErrorResponses
where
model GenericOKResponse<T> {
...OkResponse;
@header contentType: "application/json";
@bodyRoot
body: T
}
model ListResponse is GenericOKResponse<ListItems>
alias TypicalGetErrorResponses = UnauthorizedResponse | NotFoundResponse | BadRequestResponse
When adding an @opExample decorator to such an operation, the compiler crashes:
Emitter "@typespec/openapi3" crashed! This is a bug.
Please file an issue at https://github.com/microsoft/typespec/issues
TypeError: Cannot read properties of undefined (reading 'get')
at serializeObjectValueAsJson (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/compiler/src/lib/examples.ts:45:40)
at serializeValueAsJson (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/compiler/src/lib/examples.ts:32:14)
at getExampleOrExamples (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:1206:18)
at getExamplesForBodyContentEntry (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:1183:12)
at getBodyContentEntry (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:1234:14)
at emitResponseContent (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:1038:26)
at emitResponseObject (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:994:5)
at emitResponses (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:946:9)
at emitOperation (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:920:5)
at getOpenApiFromVersion (file:///Users/dmytrol/externalapi.definitions/node_modules/@typespec/openapi3/src/openapi.ts:771:11)
Reproduction
I've launched compilation under the debugger and here're my observations:
The "offending" line (in transpiled JavaScript) is const definition = type.properties.get(propValue.name);
The value of the type parameter in the call to serializeObjectValueAsJson is { kind: 'Union, ... }`
The value of the type parameter does not have the properties property defined (quite expected IMO because "Union" is not a leaf type in a syntax tree)
I would run the risk to assume that getExamplesForBodyContentEntry should probably perform an additional "walk" over the operation's return type if the return type is a union and substitute the return type passed to serializeObjectValueAsJson with the one matching the data structure provided in returnType in the decorator itself.
Alternatively (this could be easier to implement IMO) the decorator could probably allow specifying the exact return type for a specific operation example.
fix#3874 (Examples not working with nested model in array)
fix#3872 (Examples not working when type is union)
Additionally found the following issues that this fix:
- not working when type was `unknown`
- not working with `Record`
Describe the bug
Consider an operation defined like this:
@get op list(param: string): ListResponse | TypicalErrorResponses
where
When adding an
@opExample
decorator to such an operation, the compiler crashes:Reproduction
I've launched compilation under the debugger and here're my observations:
const definition = type.properties.get(propValue.name);
type
parameter in the call toserializeObjectValueAsJson
is{ kind: 'Union
, ... }`type
parameter does not have theproperties
property defined (quite expected IMO because "Union" is not a leaf type in a syntax tree)I would run the risk to assume that
getExamplesForBodyContentEntry
should probably perform an additional "walk" over the operation's return type if the return type is a union and substitute the return type passed toserializeObjectValueAsJson
with the one matching the data structure provided inreturnType
in the decorator itself.Alternatively (this could be easier to implement IMO) the decorator could probably allow specifying the exact return type for a specific operation example.
Checklist
The text was updated successfully, but these errors were encountered: