Skip to content

[OpenAPI3] @example decorator not supported for unions #4869

@RichardCPoint

Description

@RichardCPoint

Describe the bug

The @typespec/openapi3 emitter supports @examples for models, but outputs incorrect examples for unions.

For example (playground link):

@example(#{ a: 1 })
union U {
  number: {
    a: integer,
  },
  text: {
    s: string,
  },
}

should output with { a: 1 } as the example, but instead it is {}:

    U:
      anyOf:
        - type: object
          properties:
            a:
              type: integer
          required:
            - a
        - type: object
          properties:
            s:
              type: string
          required:
            - s
      example: {}

(The TypeSpec compiler correctly detects any errors in the @example tag, e.g. using @example(#{ a: "string" }) causes a compile error – so presumably the issue lies in the @typespec/openapi3 package.)

Reproduction

Playground link:
https://typespec.io/playground/?c=QGV4YW1wbGUoI3sgYTogMSB9KQptb2RlbCBBIHsKIMQUaW50ZWdlcjsKfQoK1C91bmlvbiBVxS9udW1iZXI6xQzMPSwKICB9xAV0ZXh0yB9zOiBzdHJpbmfHHn0K&e=%40typespec%2Fopenapi3&options=%7B%7D

Or example:

@example(#{ a: 1 })
union U {
  number: {
    a: integer,
  },
  text: {
    s: string,
  },
}

Checklist

Metadata

Metadata

Assignees

Labels

compiler:coreIssues for @typespec/compilerfeatureNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions