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

JsonSchema: Fix @maxValueExclusive setting minimumExclusive instead of maximumExclusive #2654

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/json-schema",
"comment": "JsonSchema: Fix `@maxValueExclusive` setting `minimumExclusive` instead of `maximumExclusive`",
"type": "none"
}
],
"packageName": "@typespec/json-schema"
}
2 changes: 1 addition & 1 deletion packages/json-schema/src/json-schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
applyConstraint(getMinValue, "minimum");
applyConstraint(getMinValueExclusive, "exclusiveMinimum");
applyConstraint(getMaxValue, "maximum");
applyConstraint(getMaxValueExclusive, "exclusiveMinimum");
applyConstraint(getMaxValueExclusive, "exclusiveMaximum");
applyConstraint(getPattern, "pattern");
applyConstraint(getMinItems, "minItems");
applyConstraint(getMaxItems, "maxItems");
Expand Down
32 changes: 31 additions & 1 deletion packages/json-schema/test/scalar-constraints.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import assert, { strictEqual } from "assert";
import { emitSchema } from "./utils.js";

describe("jsonschema: scalar constraints", () => {
Expand Down Expand Up @@ -53,6 +53,36 @@ describe("jsonschema: scalar constraints", () => {
});
});

describe("@minValueExclusive/@maxValueExclusive", () => {
for (const numType of scalarNumberTypes) {
it(numType, async () => {
const schemas = await emitSchema(
`
@minValueExclusive(1)
@maxValueExclusive(2)
scalar Test extends ${numType};
`
);

strictEqual(schemas["Test.json"].exclusiveMinimum, 1);
strictEqual(schemas["Test.json"].exclusiveMaximum, 2);
});

it("can be applied on a union", async () => {
const schemas = await emitSchema(
`
@minValueExclusive(1)
@maxValueExclusive(2)
union Test {int32, string, null};
`
);

strictEqual(schemas["Test.json"].exclusiveMinimum, 1);
strictEqual(schemas["Test.json"].exclusiveMaximum, 2);
});
}
});

describe("on property", () => {
for (const numType of [...scalarNumberTypes, "int32 | string | null"]) {
it(`handles ${numType} properties`, async () => {
Expand Down