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

Fix json schema union crash #5189

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

chrisradek
Copy link
Member

@chrisradek chrisradek commented Nov 25, 2024

Fixes #5077

This PR fixes an issue where passing in a literal union to a template would cause the @typespec/json-schema package to crash:
playground

In this scenario, the template instantiation will be inlined - similar to what the openapi3 emitter does. However, the scope of the parent referencing the model literal wasn't being preserved.

So where this used to crash:

import "@typespec/json-schema";
using TypeSpec.JsonSchema;

@jsonSchema
namespace Example;

model Foo {
  type: Template<Foo | null>;
}

model Template<T> {
  prop: T;
}

This would now generate the output:

$schema: https://json-schema.org/draft/2020-12/schema
$id: Foo.yaml
type: object
properties:
  type:
    type: object
    properties:
      prop:
        anyOf:
          - $ref: Foo.yaml
          - type: "null"
    required:
      - prop
required:
  - type

@azure-sdk
Copy link
Collaborator

All changed packages have been documented.

  • @typespec/json-schema
Show changes

@typespec/json-schema - fix ✏️

Fixes crash that occurred when a template instantiation's template argument was a union that references a declaration.

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs

@chrisradek chrisradek added this pull request to the merge queue Nov 25, 2024
Merged via the queue into microsoft:main with commit b81c9df Nov 25, 2024
23 checks passed
@chrisradek chrisradek deleted the fix-json-schema-union-crash branch November 25, 2024 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Error: Emit context must have a scope set in order to create references to declarations.
3 participants