Skip to content

Commit

Permalink
Fix handling decorators for union variants in JSONSchema emitter (#3398)
Browse files Browse the repository at this point in the history
Fixes #3391

Also refactored a minor problem of using a deprecated reexport of
`DuplicateTracker`, not it's referenced from `@typespec/compiler/utils`
directly, and another small typo.

---------

Co-authored-by: Timothee Guerin <timothee.guerin@outlook.com>
Co-authored-by: Timothee Guerin <tiguerin@microsoft.com>
  • Loading branch information
3 people committed May 20, 2024
1 parent d14b0d7 commit ccd67cf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: internal
packages:
- "@typespec/compiler"
---
Doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/json-schema"
---

Fix decorators application for union variants
2 changes: 1 addition & 1 deletion packages/compiler/src/emitter-framework/type-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export type EmitterOutput<T> = EmitEntity<T> | Placeholder<T> | T;
* literals, and any type referenced inside anywhere inside the model and any
* of the referenced types' references.
*
* In both cases, context is an object. It strongly recommended that the context
* In both cases, context is an object. It's strongly recommended that the context
* object either contain only primitive types, or else only reference immutable
* objects.
*
Expand Down
13 changes: 10 additions & 3 deletions packages/json-schema/src/json-schema-emitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
BooleanLiteral,
DiagnosticTarget,
DuplicateTracker,
Enum,
EnumMember,
IntrinsicType,
Expand Down Expand Up @@ -52,6 +51,7 @@ import {
SourceFileScope,
TypeEmitter,
} from "@typespec/compiler/emitter-framework";
import { DuplicateTracker } from "@typespec/compiler/utils";
import { stringify } from "yaml";
import {
JsonSchemaDeclaration,
Expand Down Expand Up @@ -328,7 +328,14 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
}

unionVariant(variant: UnionVariant): EmitterOutput<object> {
return this.emitter.emitTypeReference(variant.type);
const variantType = this.emitter.emitTypeReference(variant.type);
compilerAssert(variantType.kind === "code", "Unexpected non-code result from emit reference");

const result = new ObjectBuilder(variantType.value);

this.#applyConstraints(variant, result);

return result;
}

modelPropertyReference(property: ModelProperty): EmitterOutput<object> {
Expand Down Expand Up @@ -513,7 +520,7 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
}

#applyConstraints(
type: Scalar | Model | ModelProperty | Union | Enum,
type: Scalar | Model | ModelProperty | Union | UnionVariant | Enum,
schema: ObjectBuilder<unknown>
) {
const applyConstraint = (fn: (p: Program, t: Type) => any, key: string) => {
Expand Down
35 changes: 35 additions & 0 deletions packages/json-schema/test/unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,39 @@ describe("emitting unions", () => {
const Foo = schemas["Foo.json"];
assert.strictEqual(Foo["x-foo"], true);
});

it("handles decorators on variants", async () => {
const schemas = await emitSchema(`
union Foo {
@doc("doc text")
@summary("summary text")
@extension("x-key", Json<"x-value">)
bar: string;
@doc("other model doc")
@summary("other model summary")
@extension("x-key-2", Json<"x-value-2">)
baz: OtherModel;
}
model OtherModel {}
`);

const Foo = schemas["Foo.json"];

assert.deepStrictEqual(Foo.anyOf, [
{
description: "doc text",
title: "summary text",
type: "string",
"x-key": "x-value",
},
{
$ref: "OtherModel.json",
description: "other model doc",
title: "other model summary",
"x-key-2": "x-value-2",
},
]);
});
});

0 comments on commit ccd67cf

Please sign in to comment.