Skip to content

Commit

Permalink
Respect encoded name for examples (#4551)
Browse files Browse the repository at this point in the history
fix #4540
  • Loading branch information
timotheeguerin authored Sep 26, 2024
1 parent c44e785 commit df0d847
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Json serialization of example respect `@encodedName`
8 changes: 6 additions & 2 deletions packages/compiler/src/lib/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type Type,
type Value,
} from "../core/types.js";
import { getEncode, type EncodeData } from "./decorators.js";
import { getEncode, resolveEncodedName, type EncodeData } from "./decorators.js";

/**
* Serialize the given TypeSpec value as a JSON object using the given type and its encoding annotations.
Expand Down Expand Up @@ -99,7 +99,11 @@ function serializeObjectValueAsJson(
for (const propValue of value.properties.values()) {
const definition = getPropertyOfType(type, propValue.name);
if (definition) {
obj[propValue.name] = serializeValueAsJson(program, propValue.value, definition);
const name =
definition.kind === "ModelProperty"
? resolveEncodedName(program, definition, "application/json")
: propValue.name;
obj[name] = serializeValueAsJson(program, propValue.value, definition);
}
}
return obj;
Expand Down
14 changes: 14 additions & 0 deletions packages/compiler/test/decorators/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ describe("json serialization of examples", () => {
return serializeValueAsJson(program, examples[0].value, target);
}

it("respect json encodedName", async () => {
const result = await getJsonValueOfExample(`
@example(#{
expireIn: 1
})
@test model test {
@encodedName("application/json", "exp")
expireIn: int32
}
`);

expect(result).toEqual({ exp: 1 });
});

describe("scalar encoding", () => {
const allCases: [
string,
Expand Down

0 comments on commit df0d847

Please sign in to comment.