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

Support serialization of object examples on unions #5180

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

RichardCPoint
Copy link

@RichardCPoint RichardCPoint commented Nov 23, 2024

This PR fixes this issue:

Currently, 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 {}.

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.

@RichardCPoint
Copy link
Author

@RichardCPoint please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

@RichardCPoint RichardCPoint force-pushed the rejc2/support-serialization-of-object-examples-on-unions branch from 4cc2335 to 2f8d537 Compare November 24, 2024 06:27
@chrisradek
Copy link
Member

@RichardCPoint Thank you opening this pull request!

This fix looks great! Do you mind running npx chronus add from the repo root to create a changelog entry for this change? I'm also happy to run and push to your branch to add the changelog.

@RichardCPoint
Copy link
Author

Do you mind running npx chronus add from the repo root to create a changelog entry for this change?

Thanks for the review! I've run the command above and committed the result – please let me know if there's anything else I should do.

@chrisradek chrisradek enabled auto-merge December 9, 2024 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:core Issues for @typespec/compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants