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: Passing const of model type to @example #4574

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
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
Loading