Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Using the @opExample decorator crashes the compiler if the operation return type is a union #3872

Closed
4 tasks done
dmytrol opened this issue Jul 17, 2024 · 1 comment · Fixed by #3875
Closed
4 tasks done
Assignees
Labels
bug Something isn't working needs-area

Comments

@dmytrol
Copy link

dmytrol commented Jul 17, 2024

Describe the bug

Consider an operation defined like this:

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

Checklist

@dmytrol dmytrol added the bug Something isn't working label Jul 17, 2024
@timotheeguerin
Copy link
Member

Ouch, sorry about that I meant to implement that and left on leave and forgot I didn't, I'll try to get a hotfix out

@timotheeguerin timotheeguerin self-assigned this Jul 17, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 17, 2024
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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-area
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants