Files
reverse-engineering-anything/src/domain/apiContract/apiContract.ts
T
d318d752a5 feat(api-contract): resolve same-document OpenAPI path item references (#15966)
* feat(api-contract): resolve same-document OpenAPI path item references

Refs #14392.

* fix(api-contract): deduplicate shared declaration diagnostics

* fix(api-contract): preserve operations from unresolved reference chains

* fix(api-contract): preserve and deduplicate source diagnostics

* perf(api-contract): avoid temporary reference counting work

* fix(api-contract): locate malformed shared path references

* fix(api-contract): preserve reference failure reasons in narrow pages

* test(api-contract): assert narrow pages without assuming union fields

* fix(api-contract): diagnose malformed references in conflicting path items

* feat(api-contract): filter path item references by resolution state

Once same-document Path Items resolve, a contract can hold thousands of
resolved references beside a few unresolved ones. Finding those required
paging every reference. The path-item-references collection now accepts
states, and the partial-coverage unknown names the selection to use.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

---------

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-12-03 12:23:04 +08:00

107 lines
3.7 KiB
TypeScript

import { z } from "zod";
import { digestSchema, prefixedDigestSchema } from "../digests.js";
import { localPathStringSchema } from "../localPath.js";
/** Resource allowances, not OpenAPI format limits. */
export const API_CONTRACT_MAX_FILE_BYTES = 32 * 1024 * 1024;
export const API_CONTRACT_FACT_BYTES = 32 * 1024 * 1024;
export const API_CONTRACT_OPERATION = "inspect_api_contract" as const;
export const API_CONTRACT_PROVIDER = {
id: "rea-api-contract",
name: "REA offline API contract inventory",
version: "1",
} as const;
export const apiContractArtifactSchema = z.strictObject({
path: localPathStringSchema,
sha256: digestSchema,
bytes: z.int().nonnegative(),
});
/** Identity is source-scoped; operation_key separately survives source revisions. */
export const apiContractOperationSchema = z.strictObject({
operation_id: prefixedDigestSchema("api_operation"),
operation_key: prefixedDigestSchema("api_operation_key"),
method: z.string(),
path: z.string(),
declared_id: z.string().nullable(),
source_pointer: z.string(),
state: z.enum(["declared", "unknown"]),
reason: z
.enum(["unresolved-path-item-reference", "malformed-operation"])
.nullable(),
});
export const apiContractPathReferenceSchema = z.strictObject({
reference_id: prefixedDigestSchema("api_path_reference"),
path: z.string(),
source_pointer: z.string(),
reference: z.string().nullable(),
credential_redacted: z.boolean(),
state: z.enum(["unresolved", "malformed", "resolved"]),
target_pointer: z.string().optional(),
reason: z
.enum([
"external-reference",
"invalid-fragment",
"missing-target",
"invalid-target",
"cyclic-reference",
"conflicting-fields",
])
.optional(),
});
export const apiContractDiagnosticSchema = z.strictObject({
diagnostic_id: prefixedDigestSchema("api_contract_diagnostic"),
source_pointer: z.string(),
code: z.enum([
"invalid-info",
"invalid-paths",
"invalid-path-key",
"invalid-path-item",
"invalid-path-reference",
"invalid-operation",
"invalid-operation-id",
"unknown-path-item-field",
"invalid-additional-operations",
"invalid-additional-method",
]),
message: z.string(),
});
export const apiContractSchema = z.strictObject({
contract_id: prefixedDigestSchema("api_contract"),
format: z.literal("openapi-json"),
openapi: z.string(),
artifact: apiContractArtifactSchema,
info: z.strictObject({
title: z.string().nullable(),
version: z.string().nullable(),
}),
operations: z.array(apiContractOperationSchema),
path_item_references: z.array(apiContractPathReferenceSchema),
diagnostics: z.array(apiContractDiagnosticSchema),
summary: z.strictObject({
operation_slots: z.int().nonnegative(),
declared: z.int().nonnegative(),
unknown: z.int().nonnegative(),
unresolved_path_items: z.int().nonnegative(),
diagnostics: z.int().nonnegative(),
}),
source_coverage: z.strictObject({
scope: z.enum(["inline-path-operations", "path-operations"]),
status: z.enum(["complete-within-scope", "partial"]),
paths_state: z.enum(["object", "absent", "malformed"]),
path_entries_examined: z.int().nonnegative(),
reference_resolution: z.enum(["not-attempted", "same-document-path-items"]),
schema_validation: z.literal("not-performed"),
duplicate_json_members: z.literal("last-value-wins"),
}),
limitations: z.array(z.string()),
});
export type ApiContract = z.output<typeof apiContractSchema>;
export type ApiContractOperation = z.output<typeof apiContractOperationSchema>;
export type ApiContractDiagnostic = z.output<
typeof apiContractDiagnosticSchema
>;
export type ApiContractArtifact = z.output<typeof apiContractArtifactSchema>;