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
24 changes: 22 additions & 2 deletions packages/cli/generator/flag-names-cli-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ function staleness(
if (field === undefined) {
return "field missing";
}
if (field["type"] !== entry.type) {
return `field type is now ${JSON.stringify(field["type"])}`;
const type = fieldType(openapi, field);
if (type !== entry.type) {
return `field type is now ${JSON.stringify(type)}`;
}
const overrides = operation["x-forge-params"] as Overrides | undefined;
if (overrides?.[entry.wire]?.["flagName"] !== undefined) {
Expand All @@ -103,6 +104,25 @@ function bodyField(
return deref(openapi, record(schema?.["properties"])?.[wire]);
}

function fieldType(
openapi: ForgeOpenApiDocument,
field: Record<string, unknown>
): unknown {
let schema: Record<string, unknown> | undefined = field;
for (let depth = 0; depth < 10 && schema !== undefined; depth++) {
if (schema["type"] !== undefined) {
return schema["type"];
}
// Forge can wrap a reference in allOf to attach property annotations.
const allOf = schema["allOf"];
if (!Array.isArray(allOf) || allOf.length !== 1) {
return undefined;
}
schema = deref(openapi, allOf[0]);
}
return undefined;
}

function deref(
openapi: ForgeOpenApiDocument,
value: unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ describe("applyTemporaryFlagNames", () => {
});
});

it.each([
{ type: "string" },
{ $ref: "#/components/schemas/precursor_mode" },
{ allOf: [{ $ref: "#/components/schemas/precursor_mode" }] },
])("names a field wrapped in a single allOf: %j", (schema) => {
const doc = document();
const schemas = doc.components?.["schemas"] as Record<string, object>;
const mode = {
allOf: [schema],
"x-fern-property-name": "precursor-mode",
};
schemas["precursor_config"] = {
type: "object",
properties: { mode },
};

applyTemporaryFlagNames(doc);

expect(overrides(doc, PRECURSOR, "put")).toEqual({
mode: { flagName: "detection-mode" },
});
expect(mode).toEqual({
allOf: [schema],
"x-fern-property-name": "precursor-mode",
});
});

it.each<[string, (doc: ForgeOpenApiDocument) => void, string]>([
[
"a missing operation",
Expand Down Expand Up @@ -106,6 +133,24 @@ describe("applyTemporaryFlagNames", () => {
},
'field type is now "integer"',
],
[
"a retyped field wrapped in allOf",
(doc) => {
const schemas = doc.components?.["schemas"] as Record<string, object>;
schemas["precursor_mode"] = { allOf: [{ type: "integer" }] };
},
'field type is now "integer"',
],
[
"a field with conflicting allOf types",
(doc) => {
const schemas = doc.components?.["schemas"] as Record<string, object>;
schemas["precursor_mode"] = {
allOf: [{ type: "string" }, { type: "integer" }],
};
},
"field type is now undefined",
],
[
"an upstream flagName",
(doc) => {
Expand Down
Loading