Skip to content
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
21 changes: 21 additions & 0 deletions src/provider-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,34 @@ function stripProviderUnsupportedSchemaKeywords(value: unknown): unknown {
}
output[key] = stripProviderUnsupportedSchemaKeywords(item);
}
const nullableType = nullableAnyOfType(output["anyOf"]);
if (nullableType !== null && Object.keys(output).length === 1) {
output["type"] = [nullableType, "null"];
delete output["anyOf"];
}
if (output["type"] === "object" && isRecord(output["properties"])) {
output["additionalProperties"] = false;
output["required"] = Object.keys(output["properties"]);
}
return output;
}

function nullableAnyOfType(value: unknown): string | null {
if (!Array.isArray(value) || value.length !== 2) {
return null;
}
const typeSchemas = value.filter(isSingleTypeSchema);
const nullable = typeSchemas.find((schema) => schema["type"] === "null");
const typed = typeSchemas.find((schema) => schema["type"] !== "null");
return typeSchemas.length === 2 && nullable !== undefined && typeof typed?.["type"] === "string"
? typed["type"]
: null;
}

function isSingleTypeSchema(value: unknown): value is { type: string } {
return isRecord(value) && typeof value["type"] === "string" && Object.keys(value).length === 1;
}

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
14 changes: 14 additions & 0 deletions src/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,20 @@ describe("providerJsonSchema", () => {
}
});

it("normalizes nullable evidence line schemas for Codex strict schemas", () => {
const schema = reviewJsonSchema as Record<string, unknown>;
const findings = propertySchema(schema, "findings");
const finding = itemSchema(findings);
const evidence = itemSchema(propertySchema(finding, "evidence"));
const endLine = propertySchema(evidence, "endLine");

expect(endLine["anyOf"]).toBeUndefined();
expect(endLine["type"]).toEqual(["integer", "null"]);
expect(schemaKeys(endLine)).not.toContain("minimum");
expect(evidence["additionalProperties"]).toBe(false);
expect(evidence["required"]).toEqual(Object.keys(propertiesOf(evidence)));
});

it("keeps object schemas strict even when parser input fields are optional", () => {
const schema = providerJsonSchema(reviewOutputSchema) as Record<string, unknown>;
const findings = propertySchema(schema, "findings");
Expand Down