Skip to content

Commit

Permalink
Fix: Passing const of model type to @example (#4574)
Browse files Browse the repository at this point in the history
fix #4544
  • Loading branch information
timotheeguerin authored Oct 4, 2024
1 parent 68c94a7 commit e80bdce
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .chronus/changes/fix-examples-const-2024-8-30-17-28-17.md
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/compiler"
---

Fix: Passing `const` of model type to `@example`
12 changes: 12 additions & 0 deletions docs/standard-library/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ model Pet {
}
```

### Define typed examples using `const`

```tsp
const petExample: Pet = #{ name: "Max", age: 3 };
@example(petExample)
model Pet {
name: string;
age: int32;
}
```

## Operation examples

Operation example are provided with the `@opExample` decorator. Similar to the `@example` decorator the first argument is the example value however it takes both the `parameters` and `returnType` example.
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3921,7 +3921,6 @@ export function createChecker(program: Program): Checker {
type.indexer = isBase.indexer;
}
}
decorators.push(...checkDecorators(type, node, mapper));

if (isBase) {
for (const prop of isBase.properties.values()) {
Expand Down Expand Up @@ -3957,6 +3956,8 @@ export function createChecker(program: Program): Checker {
// Evaluate the properties after
checkModelProperties(node, type.properties, type, mapper);

decorators.push(...checkDecorators(type, node, mapper));

linkMapper(type, mapper);

if (shouldCreateTypeForTemplate(node, mapper)) {
Expand Down
36 changes: 36 additions & 0 deletions packages/compiler/test/decorators/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ describe("@example", () => {
expect(serializeValueAsJson(program, examples[0].value, target)).toEqual({ a: 1, b: 2 });
});

it("use const with type of model", async () => {
const { program, examples, target } = await getExamplesFor(`
const example: Test = #{ a: 1, b: 2 };
@example(example)
@test("test") model Test {
a: int32;
b: int32;
}
`);
expect(examples).toHaveLength(1);
expect(serializeValueAsJson(program, examples[0].value, target)).toEqual({ a: 1, b: 2 });
});

it("emit diagnostic for missing property", async () => {
const diagnostics = await diagnoseCode(`
@example(#{ a: 1 })
Expand Down Expand Up @@ -98,6 +111,16 @@ describe("@example", () => {
expect(serializeValueAsJson(program, examples[0].value, target)).toEqual("11:32");
});

it("use const with type of scalar", async () => {
const { program, examples, target } = await getExamplesFor(`
const example: test = test.fromISO("11:32");
@example(example)
@test scalar test extends utcDateTime;
`);
expect(examples).toHaveLength(1);
expect(serializeValueAsJson(program, examples[0].value, target)).toEqual("11:32");
});

it("emit diagnostic for unassignable value", async () => {
const diagnostics = await diagnoseCode(`
@example("11:32")
Expand All @@ -122,6 +145,19 @@ describe("@example", () => {
expect(serializeValueAsJson(program, examples[0].value, target)).toEqual("a");
});

it("use const with type of enum", async () => {
const { program, examples, target } = await getExamplesFor(`
const example: Test = Test.a;
@example(example)
@test("test") enum Test {
a,
b,
}
`);
expect(examples).toHaveLength(1);
expect(serializeValueAsJson(program, examples[0].value, target)).toEqual("a");
});

it("emit diagnostic for unassignable value", async () => {
const diagnostics = await diagnoseCode(`
@example(1)
Expand Down

0 comments on commit e80bdce

Please sign in to comment.