Skip to content

Commit 28e16e1

Browse files
Увалиев Султан Ермековичfactory-droid[bot]
authored andcommitted
test(effect-acp): regression test keeps the anyOf property in generated schemas
Extract the testable schema transform pipeline (collectSchemaEntries, mapSubschemas, normalizeSchemaKeywords, flattenCompositions, generateSchemaOutput) from scripts/generate.ts into scripts/schemaTransform.ts and add a generator-level regression test: a schema schema containing a property named anyOf survives the traversal and generates a real property instead of Schema.Never. The checked-in generated schemas and the replay test are not used as fixtures. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
1 parent 1a5c176 commit 28e16e1

3 files changed

Lines changed: 389 additions & 326 deletions

File tree

‎packages/effect-acp/scripts/generate.ts‎

Lines changed: 2 additions & 326 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
44
import * as NodeServices from "@effect/platform-node/NodeServices";
5-
import { make as makeJsonSchemaGenerator } from "@effect/openapi-generator/JsonSchemaGenerator";
65
import * as Effect from "effect/Effect";
76
import * as FileSystem from "effect/FileSystem";
87
import * as Layer from "effect/Layer";
@@ -13,6 +12,8 @@ import { Command, Flag } from "effect/cli";
1312
import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http";
1413
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1514

15+
import { generateSchemaOutput } from "./schemaTransform.ts";
16+
1617
const V2_SCHEMA_RELEASE = "schema-v2.0.0-alpha.3";
1718
const V1_SCHEMA_RELEASE = "schema-v1.21.0";
1819

@@ -127,331 +128,6 @@ const writeGeneratedFiles = Effect.fn("writeGeneratedFiles")(function* (
127128
yield* fs.writeFileString(metaOutputPath, metaOutput);
128129
});
129130

130-
function collectSchemaEntries(
131-
chunk: string,
132-
): ReadonlyArray<{ readonly name: string; readonly code: string }> {
133-
const lines = chunk
134-
.split("\n")
135-
.map((line) => line.trim())
136-
.filter((line) => line.length > 0 && !line.startsWith("//"));
137-
const entries: Array<{ name: string; code: string }> = [];
138-
139-
for (let index = 0; index < lines.length; index += 1) {
140-
const typeLine = lines[index];
141-
if (!typeLine?.startsWith("export type ")) {
142-
continue;
143-
}
144-
145-
const constLine = lines[index + 1];
146-
if (!constLine?.startsWith("export const ")) {
147-
throw new Error(`Malformed generator output near: ${typeLine}`);
148-
}
149-
150-
const match = /^export type ([A-Za-z0-9_]+)/.exec(typeLine);
151-
if (!match?.[1]) {
152-
throw new Error(`Could not extract schema name from: ${typeLine}`);
153-
}
154-
155-
entries.push({
156-
name: match[1],
157-
code: `${typeLine}\n${constLine}`,
158-
});
159-
index += 1;
160-
}
161-
162-
return entries;
163-
}
164-
165-
function isJsonArray(value: Schema.Json | undefined): value is ReadonlyArray<Schema.Json> {
166-
return Array.isArray(value);
167-
}
168-
169-
// Keywords whose values are literal JSON data, not schemas.
170-
const DATA_KEYWORDS = new Set(["const", "default", "enum", "examples"]);
171-
172-
// Applies `transform` to every subschema of a schema object. `properties` maps names to
173-
// schemas, so its keys stay property names: ACP has properties named `anyOf`, `oneOf`, and
174-
// `type` that must not be read as keywords. Literal data values are left untouched.
175-
function mapSubschemas(
176-
value: { readonly [key: string]: Schema.Json },
177-
transform: (schema: Schema.Json) => Schema.Json,
178-
): Record<string, Schema.Json> {
179-
return Object.fromEntries(
180-
Object.entries(value).map(([key, child]) => {
181-
if (DATA_KEYWORDS.has(key)) return [key, child];
182-
if (
183-
key === "properties" &&
184-
child !== null &&
185-
typeof child === "object" &&
186-
!isJsonArray(child)
187-
) {
188-
return [
189-
key,
190-
Object.fromEntries(
191-
Object.entries(child).map(([name, schema]) => [name, transform(schema)]),
192-
),
193-
];
194-
}
195-
return [key, isJsonArray(child) ? child.map(transform) : transform(child)];
196-
}),
197-
);
198-
}
199-
200-
function normalizeSchemaKeywords(value: Schema.Json): Schema.Json {
201-
if (isJsonArray(value)) {
202-
return value.map(normalizeSchemaKeywords);
203-
}
204-
if (value === null || typeof value !== "object") {
205-
return value;
206-
}
207-
208-
const normalizedObject = mapSubschemas(value, normalizeSchemaKeywords);
209-
// ACP's forward-compatible variants exclude known discriminator values.
210-
// The Effect generator does not support `not`; express this exact exclusion
211-
// as a string pattern while preserving validation of the known variants.
212-
if (normalizedObject.not !== undefined) {
213-
const exclusion = normalizedObject.not;
214-
if (
215-
exclusion === null ||
216-
typeof exclusion !== "object" ||
217-
isJsonArray(exclusion) ||
218-
!isJsonArray(exclusion.anyOf)
219-
) {
220-
throw new Error("Unsupported ACP schema exclusion");
221-
}
222-
const excluded = exclusion.anyOf.map((variant) => {
223-
if (variant === null || typeof variant !== "object" || isJsonArray(variant))
224-
throw new Error("Invalid ACP excluded variant");
225-
const properties = variant.properties;
226-
if (properties === null || typeof properties !== "object" || isJsonArray(properties))
227-
throw new Error("Invalid ACP excluded properties");
228-
const entries = Object.entries(properties);
229-
const entry = entries[0];
230-
if (entries.length !== 1 || entry === undefined) throw new Error("Invalid ACP discriminator");
231-
const [key, schema] = entry;
232-
if (
233-
schema === null ||
234-
typeof schema !== "object" ||
235-
isJsonArray(schema) ||
236-
typeof schema.const !== "string"
237-
)
238-
throw new Error("Invalid ACP discriminator value");
239-
return { key, value: schema.const };
240-
});
241-
const discriminator = excluded[0]?.key;
242-
const properties = normalizedObject.properties;
243-
if (
244-
discriminator === undefined ||
245-
excluded.some((entry) => entry.key !== discriminator) ||
246-
properties === null ||
247-
typeof properties !== "object" ||
248-
isJsonArray(properties)
249-
)
250-
throw new Error("Unsupported ACP discriminator exclusion");
251-
const property = properties[discriminator];
252-
if (property === null || typeof property !== "object" || isJsonArray(property))
253-
throw new Error("Missing ACP discriminator property");
254-
normalizedObject.properties = {
255-
...properties,
256-
[discriminator]: {
257-
...property,
258-
pattern: `^(?!(?:${excluded.map(({ value }) => RegExp.escape(value)).join("|")})$)[\\s\\S]*$`,
259-
},
260-
};
261-
delete normalizedObject.not;
262-
}
263-
if (normalizedObject.unevaluatedProperties !== undefined) {
264-
if (normalizedObject.unevaluatedProperties !== true)
265-
throw new Error("Unsupported ACP unevaluated properties constraint");
266-
normalizedObject.additionalProperties = true;
267-
delete normalizedObject.unevaluatedProperties;
268-
}
269-
if (isJsonArray(normalizedObject.oneOf)) {
270-
const discriminator = normalizedObject.discriminator;
271-
const propertyName =
272-
discriminator !== null && typeof discriminator === "object" && !isJsonArray(discriminator)
273-
? discriminator.propertyName
274-
: undefined;
275-
const values = normalizedObject.oneOf.map((entry) => {
276-
if (entry === null || typeof entry !== "object" || isJsonArray(entry)) return undefined;
277-
if (typeof entry.const === "string") return entry.const;
278-
const properties = entry.properties;
279-
if (
280-
typeof propertyName !== "string" ||
281-
properties === null ||
282-
typeof properties !== "object" ||
283-
isJsonArray(properties)
284-
)
285-
return undefined;
286-
const property = properties[propertyName];
287-
return property !== null && typeof property === "object" && !isJsonArray(property)
288-
? property.const
289-
: undefined;
290-
});
291-
if (
292-
values.every((entry) => typeof entry === "string") &&
293-
new Set(values).size === values.length
294-
) {
295-
normalizedObject.anyOf = normalizedObject.oneOf;
296-
delete normalizedObject.oneOf;
297-
}
298-
}
299-
const typeValue = normalizedObject.type;
300-
301-
if (!isJsonArray(typeValue)) {
302-
return normalizedObject;
303-
}
304-
305-
const normalizedTypes = typeValue.filter((entry): entry is string => typeof entry === "string");
306-
if (normalizedTypes.length !== typeValue.length || !normalizedTypes.includes("null")) {
307-
return normalizedObject;
308-
}
309-
310-
const nonNullTypes = normalizedTypes.filter((entry) => entry !== "null");
311-
if (nonNullTypes.length !== 1) {
312-
return normalizedObject;
313-
}
314-
const nonNullType = nonNullTypes[0]!;
315-
316-
const nextObject: Record<string, Schema.Json> = {};
317-
for (const [key, child] of Object.entries(normalizedObject)) {
318-
if (key !== "type") {
319-
nextObject[key] = child;
320-
}
321-
}
322-
323-
return {
324-
anyOf: [
325-
{
326-
...nextObject,
327-
type: nonNullType,
328-
},
329-
{ type: "null" },
330-
],
331-
};
332-
}
333-
334-
const isAnnotationKeyword = (key: string): boolean =>
335-
["description", "title", "$comment", "default", "examples", "discriminator"].includes(key) ||
336-
key.startsWith("x-");
337-
338-
// Distribute object intersections over unions before handing schemas to Effect.
339-
// ACP uses sibling `properties`/`anyOf` and `allOf` references for elicitation scopes.
340-
function flattenCompositions(
341-
value: Schema.Json,
342-
definitions: Readonly<Record<string, Schema.Json>>,
343-
): Schema.Json {
344-
if (isJsonArray(value)) return value.map((child) => flattenCompositions(child, definitions));
345-
if (value === null || typeof value !== "object") return value;
346-
const normalized = mapSubschemas(value, (child) => flattenCompositions(child, definitions));
347-
const { allOf, anyOf, ...base } = normalized;
348-
const merge = (left: Schema.Json, right: Schema.Json): Schema.Json => {
349-
if (
350-
left === null ||
351-
typeof left !== "object" ||
352-
isJsonArray(left) ||
353-
right === null ||
354-
typeof right !== "object" ||
355-
isJsonArray(right)
356-
)
357-
throw new Error("Unsupported ACP schema intersection");
358-
if (typeof right.$ref === "string") {
359-
// Keep references when the sibling only describes the referenced schema.
360-
// Inlining these repeats entire protocol unions in every request schema.
361-
if (Object.keys(left).every(isAnnotationKeyword)) return { ...right, ...left };
362-
const target = definitions[right.$ref.replace("#/$defs/", "")];
363-
if (target === undefined) throw new Error(`Missing ACP definition ${right.$ref}`);
364-
return merge(left, flattenCompositions(normalizeSchemaKeywords(target), definitions));
365-
}
366-
if (isJsonArray(left.anyOf)) return { anyOf: left.anyOf.map((branch) => merge(branch, right)) };
367-
if (isJsonArray(right.anyOf))
368-
return { anyOf: right.anyOf.map((branch) => merge(left, branch)) };
369-
for (const key of Object.keys(left)) {
370-
if (key === "properties" || key === "required" || isAnnotationKeyword(key)) continue;
371-
if (right[key] !== undefined && JSON.stringify(left[key]) !== JSON.stringify(right[key])) {
372-
throw new Error(`Unsupported conflicting ACP schema constraint: ${key}`);
373-
}
374-
}
375-
const result = { ...left, ...right };
376-
if (
377-
left.properties &&
378-
right.properties &&
379-
typeof left.properties === "object" &&
380-
!isJsonArray(left.properties) &&
381-
typeof right.properties === "object" &&
382-
!isJsonArray(right.properties)
383-
) {
384-
for (const key of Object.keys(left.properties)) {
385-
if (
386-
right.properties[key] !== undefined &&
387-
JSON.stringify(left.properties[key]) !== JSON.stringify(right.properties[key])
388-
) {
389-
throw new Error(`Unsupported overlapping ACP schema property: ${key}`);
390-
}
391-
}
392-
result.properties = { ...left.properties, ...right.properties };
393-
}
394-
if (isJsonArray(left.required) || isJsonArray(right.required)) {
395-
result.required = [
396-
...new Set([
397-
...(isJsonArray(left.required) ? left.required : []),
398-
...(isJsonArray(right.required) ? right.required : []),
399-
]),
400-
];
401-
}
402-
return result;
403-
};
404-
let combined: Schema.Json = base;
405-
if (isJsonArray(allOf)) for (const part of allOf) combined = merge(combined, part);
406-
if (isJsonArray(anyOf) && !isJsonArray(allOf) && Object.keys(base).every(isAnnotationKeyword)) {
407-
return { ...base, anyOf };
408-
}
409-
return isJsonArray(anyOf) ? merge(combined, { anyOf }) : combined;
410-
}
411-
412-
function generateSchemaOutput(
413-
upstreamSchema: { readonly $defs: Readonly<Record<string, Schema.Json>> },
414-
release: string,
415-
): { readonly output: string; readonly count: number } {
416-
const normalizedDefinitions = Object.fromEntries(
417-
Object.entries(upstreamSchema.$defs).map(([name, schema]) => [
418-
name,
419-
flattenCompositions(normalizeSchemaKeywords(schema), upstreamSchema.$defs),
420-
]),
421-
);
422-
const sortedEntries = Object.entries(normalizedDefinitions).toSorted(([left], [right]) =>
423-
left.localeCompare(right),
424-
);
425-
const generatedEntries = new Map<string, string>();
426-
const generator = makeJsonSchemaGenerator();
427-
428-
for (const [name, schema] of sortedEntries) {
429-
generator.addSchema(name, schema as never);
430-
}
431-
432-
const generated = generator.generate("openapi-3.1", normalizedDefinitions as never, false).trim();
433-
if (generated.length > 0) {
434-
for (const entry of collectSchemaEntries(generated)) {
435-
if (!generatedEntries.has(entry.name)) {
436-
generatedEntries.set(entry.name, entry.code);
437-
}
438-
}
439-
}
440-
441-
return {
442-
output: [
443-
"// This file is generated by the effect-acp package. Do not edit manually.",
444-
`// Current ACP schema release: ${release}`,
445-
"",
446-
'import * as Schema from "effect/Schema";',
447-
"",
448-
[...generatedEntries.values()].join("\n\n"),
449-
"",
450-
].join("\n"),
451-
count: generatedEntries.size,
452-
};
453-
}
454-
455131
const generateSchemas = Effect.fn("generateSchemas")(function* (skipDownload: boolean) {
456132
const { upstreamMetaPath, upstreamSchemaPath, upstreamV1MetaPath, upstreamV1SchemaPath } =
457133
yield* getGeneratedPaths();

0 commit comments

Comments
 (0)