Skip to content

Commit

Permalink
Stop json schema from crashing on unknown scalar (microsoft#4150)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored and weidongxu-microsoft committed Sep 3, 2024
1 parent 02ed0ba commit e92e26f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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"
---

Stop json schema from crashing on unknown scalar and handle `unixTimestamp32`
8 changes: 7 additions & 1 deletion packages/json-schema/src/json-schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
case "int16":
return { type: "integer", minimum: -32768, maximum: 32767 };
case "int32":
case "unixTimestamp32":
return { type: "integer", minimum: -2147483648, maximum: 2147483647 };
case "int64":
const int64Strategy = this.emitter.getOptions()["int64-strategy"] ?? "string";
Expand Down Expand Up @@ -529,7 +530,12 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
case "bytes":
return { type: "string", contentEncoding: "base64" };
default:
compilerAssert(false, `Unknown built-in scalar type ${baseBuiltIn.name}`);
reportDiagnostic(this.emitter.getProgram(), {
code: "unknown-scalar",
format: { name: baseBuiltIn.name },
target: baseBuiltIn,
});
return {};
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/json-schema/test/built-ins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("emitting built-in types", () => {
["int8", { type: "integer", minimum: -128, maximum: 127 }],
["int16", { type: "integer", minimum: -32768, maximum: 32767 }],
["int32", { type: "integer", minimum: -2147483648, maximum: 2147483647 }],
["unixTimestamp32", { type: "integer", minimum: -2147483648, maximum: 2147483647 }],
["int64", { type: "string" }],
["uint8", { type: "integer", minimum: 0, maximum: 255 }],
["uint16", { type: "integer", minimum: 0, maximum: 65535 }],
Expand Down

0 comments on commit e92e26f

Please sign in to comment.