oas-truth 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +53 -18
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -624,7 +624,7 @@ type SchemaReplacementsOptions = {
|
|
|
624
624
|
readonly identifiers?: ReadonlyMap<string, string>;
|
|
625
625
|
};
|
|
626
626
|
export declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter, options?: SchemaReplacementsOptions): Map<unknown, string>;
|
|
627
|
-
export declare function toPascalCase(
|
|
627
|
+
export declare function toPascalCase(text: string): string;
|
|
628
628
|
/**
|
|
629
629
|
* Extract the component name from a `$ref`, decoding percent-encoding so the
|
|
630
630
|
* result matches the (decoded) key under `components.schemas`. Pure-ASCII refs
|
package/dist/index.js
CHANGED
|
@@ -42,7 +42,7 @@ async function parseOpenAPI(input) {
|
|
|
42
42
|
const program = await compile(NodeHost, path.resolve(input), { noEmit: true });
|
|
43
43
|
if (program.diagnostics.length > 0) return {
|
|
44
44
|
ok: false,
|
|
45
|
-
error: `TypeSpec compile failed:\n${program.diagnostics.map((
|
|
45
|
+
error: `TypeSpec compile failed:\n${program.diagnostics.map((diagnostic) => diagnostic.message).join("\n")}`
|
|
46
46
|
};
|
|
47
47
|
const [record] = await getOpenAPI3(program);
|
|
48
48
|
const document = record === void 0 ? void 0 : "document" in record ? record.document : record.versions[0]?.document;
|
|
@@ -111,7 +111,7 @@ function extractParts(code, stripRefs = (s) => s) {
|
|
|
111
111
|
const firstCode = lines.findIndex((line) => line.trim() !== "" && !line.startsWith("//"));
|
|
112
112
|
const comments = (firstCode === -1 ? lines : lines.slice(0, firstCode)).filter((line) => line.startsWith("//"));
|
|
113
113
|
const joined = (firstCode === -1 ? [] : lines.slice(firstCode)).join("\n").trim();
|
|
114
|
-
const expr =
|
|
114
|
+
const expr = /^export const \w+ = (.+)$/su.exec(joined)?.[1];
|
|
115
115
|
return {
|
|
116
116
|
expression: stripRefs(expr === void 0 ? joined : expr.replace(/;?\s*$/u, "")),
|
|
117
117
|
comments
|
|
@@ -167,7 +167,7 @@ function stripEffectSuspend(code) {
|
|
|
167
167
|
* references that leave the cycle.
|
|
168
168
|
*/
|
|
169
169
|
function renderArktypeCyclic(varName, containerExpr, externals) {
|
|
170
|
-
const body =
|
|
170
|
+
const body = /scope\(\{([\s\S]*)\}\)\.export\(\)/u.exec(containerExpr)?.[1];
|
|
171
171
|
if (body === void 0) return containerExpr;
|
|
172
172
|
return `scope({${[...externals.map((ref) => `${ref}:${ref}`), body].join(",")}}).export().${varName}`;
|
|
173
173
|
}
|
|
@@ -294,8 +294,8 @@ function makeEffectAdapter() {
|
|
|
294
294
|
};
|
|
295
295
|
}
|
|
296
296
|
// src/utils/index.ts
|
|
297
|
-
function toPascalCase(
|
|
298
|
-
return
|
|
297
|
+
function toPascalCase(text) {
|
|
298
|
+
return text.replaceAll(/[^a-zA-Z0-9]+(.)/gu, (_, letter) => letter.toUpperCase()).replace(/^(.)/u, (_, letter) => letter.toUpperCase());
|
|
299
299
|
}
|
|
300
300
|
/**
|
|
301
301
|
* Decode percent-encoding, falling back to the raw input on malformed sequences
|
|
@@ -337,7 +337,7 @@ function toIdentifierPascalCase(name) {
|
|
|
337
337
|
const parts = encodeNonAscii(name).split(/[^a-zA-Z0-9]+/u).filter(Boolean);
|
|
338
338
|
if (parts.length === 0) return "Schema";
|
|
339
339
|
const result = parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
340
|
-
if (/^[0-9]/u.test(result)) return `_${result}`.replace(/([0-9])([a-z])/u, (_,
|
|
340
|
+
if (/^[0-9]/u.test(result)) return `_${result}`.replace(/([0-9])([a-z])/u, (_, digit, letter) => `${digit}${letter.toUpperCase()}`);
|
|
341
341
|
return result;
|
|
342
342
|
}
|
|
343
343
|
/**
|
|
@@ -367,7 +367,7 @@ function declarationFileName(ident) {
|
|
|
367
367
|
* the suffixed name (`User` → `User2Schema`).
|
|
368
368
|
*/
|
|
369
369
|
function makeSchemaVarName(ref, identifiers) {
|
|
370
|
-
const name =
|
|
370
|
+
const name = /^#\/components\/schemas\/(.+)$/u.exec(ref)?.[1];
|
|
371
371
|
if (name === void 0) return void 0;
|
|
372
372
|
const key = safeDecode(name);
|
|
373
373
|
return `${identifiers?.get(key) ?? toIdentifierPascalCase(key)}Schema`;
|
|
@@ -459,7 +459,7 @@ function makeEntries(v, adapter, seen, path, options) {
|
|
|
459
459
|
*/
|
|
460
460
|
function joinDeclarations(declarations, options) {
|
|
461
461
|
if (declarations.length === 0) return "";
|
|
462
|
-
const body = declarations.map((
|
|
462
|
+
const body = declarations.map((declaration) => declaration.code).join(";");
|
|
463
463
|
const text = options?.trailingNewline === true ? `${body}\n` : body;
|
|
464
464
|
const importLine = typeof options?.importLine === "function" ? options.importLine(body) : options?.importLine;
|
|
465
465
|
return importLine === void 0 ? text : `${importLine}\n\n${text}`;
|
|
@@ -470,7 +470,7 @@ function makeCallbacksDeclarations(components, adapter, options) {
|
|
|
470
470
|
if (!callbacks) return [];
|
|
471
471
|
const asConst = options?.readonly === true ? " as const" : "";
|
|
472
472
|
return Object.entries(callbacks).flatMap(([name, callback]) => {
|
|
473
|
-
if ("$ref" in callback && callback.$ref) return [];
|
|
473
|
+
if ("$ref" in callback && typeof callback.$ref === "string") return [];
|
|
474
474
|
const ident = toIdentifierPascalCase(name);
|
|
475
475
|
const varName = `${ident}Callback`;
|
|
476
476
|
const replacements = makeSchemaReplacements(callback, adapter, {
|
|
@@ -512,7 +512,7 @@ function makeHeadersDeclarations(components, adapter, options) {
|
|
|
512
512
|
const { headers } = components;
|
|
513
513
|
if (!headers) return [];
|
|
514
514
|
return Object.entries(headers).flatMap(([name, header]) => {
|
|
515
|
-
if (!("schema" in header) || !header.schema) return [];
|
|
515
|
+
if (!("schema" in header) || !isRecord(header.schema)) return [];
|
|
516
516
|
const ident = toIdentifierPascalCase(name);
|
|
517
517
|
const varName = `${ident}HeaderSchema`;
|
|
518
518
|
const raw = adapter.toExpression(header.schema);
|
|
@@ -557,7 +557,7 @@ function makeMediaTypesDeclarations(components, adapter, options) {
|
|
|
557
557
|
if (!mediaTypes) return [];
|
|
558
558
|
return Object.entries(mediaTypes).flatMap(([name, media]) => {
|
|
559
559
|
if ("$ref" in media && media.$ref) return [];
|
|
560
|
-
if (!("schema" in media) || !media.schema) return [];
|
|
560
|
+
if (!("schema" in media) || !isRecord(media.schema)) return [];
|
|
561
561
|
const ident = toIdentifierPascalCase(name);
|
|
562
562
|
const varName = `${ident}MediaTypeSchema`;
|
|
563
563
|
const raw = adapter.toExpression(media.schema);
|
|
@@ -858,15 +858,15 @@ const SUBSCHEMA_MAP_KEYS = /* @__PURE__ */ new Set([
|
|
|
858
858
|
function isSchema(value) {
|
|
859
859
|
return isRecord(value);
|
|
860
860
|
}
|
|
861
|
-
/** Rewrites a schema bottom-up with `
|
|
862
|
-
function mapSchema(schema,
|
|
863
|
-
const map = (value) => isSchema(value) ? mapSchema(value,
|
|
861
|
+
/** Rewrites a schema bottom-up with `rewrite`, following only subschemas (never example or default values). */
|
|
862
|
+
function mapSchema(schema, rewrite) {
|
|
863
|
+
const map = (value) => isSchema(value) ? mapSchema(value, rewrite) : value;
|
|
864
864
|
const node = Object.fromEntries(Object.entries(schema).map(([key, value]) => {
|
|
865
865
|
if (SUBSCHEMA_KEYS.has(key)) return [key, Array.isArray(value) ? value.map(map) : map(value)];
|
|
866
866
|
if (SUBSCHEMA_MAP_KEYS.has(key) && isRecord(value)) return [key, Object.fromEntries(Object.entries(value).map(([k, v]) => [k, map(v)]))];
|
|
867
867
|
return [key, value];
|
|
868
868
|
}));
|
|
869
|
-
return
|
|
869
|
+
return rewrite(isSchema(node) ? node : schema);
|
|
870
870
|
}
|
|
871
871
|
/**
|
|
872
872
|
* `readonly` as the declarative `x-readonly` extension on every object and array,
|
|
@@ -896,11 +896,46 @@ function rewriteCollisionRef(node, identifiers) {
|
|
|
896
896
|
$ref: `#/components/schemas/${ident}`
|
|
897
897
|
};
|
|
898
898
|
}
|
|
899
|
+
/** The component key a `#/components/schemas/...` reference names; undefined for any other reference. */
|
|
900
|
+
function componentSchemaKey(ref) {
|
|
901
|
+
return ref.startsWith("#/components/schemas/") ? schemaRefToName(ref) : void 0;
|
|
902
|
+
}
|
|
899
903
|
function makeCyclicContainer(varName, group, schemas, identifiers, adapter, ref) {
|
|
900
904
|
const groupIdentifiers = new Map(group.map((name) => [name, identifiers.get(name) ?? name]));
|
|
905
|
+
const localRef = (key) => {
|
|
906
|
+
const groupIdent = groupIdentifiers.get(key);
|
|
907
|
+
return groupIdent === void 0 ? void 0 : `#/$defs/${groupIdent}Schema`;
|
|
908
|
+
};
|
|
909
|
+
const keyByLocalRef = new Map(group.map((name) => [localRef(name), name]));
|
|
910
|
+
/**
|
|
911
|
+
* A member localized to `#/$defs/<Ident>Schema` would take that name as its implicit
|
|
912
|
+
* discriminator value, so the schema name is pinned in the mapping, and mapping targets
|
|
913
|
+
* that point into the cycle follow the members to their local refs.
|
|
914
|
+
*/
|
|
915
|
+
const localizeDiscriminator = (node) => {
|
|
916
|
+
const { discriminator } = node;
|
|
917
|
+
if (discriminator?.propertyName === void 0) return node;
|
|
918
|
+
const mapping = Object.entries(discriminator.mapping ?? {});
|
|
919
|
+
const mapped = new Set(mapping.map(([, target]) => componentSchemaKey(target)));
|
|
920
|
+
const values = new Set(mapping.map(([value]) => value));
|
|
921
|
+
const implicit = [...node.oneOf ?? [], ...node.anyOf ?? []].flatMap((member) => {
|
|
922
|
+
const key = member.$ref === void 0 ? void 0 : keyByLocalRef.get(member.$ref);
|
|
923
|
+
return key === void 0 || mapped.has(key) || values.has(key) ? [] : [[key, member.$ref]];
|
|
924
|
+
});
|
|
925
|
+
const localized = mapping.map(([value, target]) => {
|
|
926
|
+
const key = componentSchemaKey(target);
|
|
927
|
+
return [value, (key === void 0 ? void 0 : localRef(key)) ?? target];
|
|
928
|
+
});
|
|
929
|
+
if (implicit.length === 0 && localized.every(([, target], index) => target === mapping[index]?.[1])) return node;
|
|
930
|
+
const local = Object.fromEntries([...Object.entries(node), ["discriminator", {
|
|
931
|
+
...discriminator,
|
|
932
|
+
mapping: Object.fromEntries([...localized, ...implicit])
|
|
933
|
+
}]]);
|
|
934
|
+
return isSchema(local) ? local : node;
|
|
935
|
+
};
|
|
901
936
|
const localize = (node) => {
|
|
902
937
|
const key = node.$ref ? schemaRefToName(node.$ref) : void 0;
|
|
903
|
-
if (key === void 0) return node;
|
|
938
|
+
if (key === void 0) return localizeDiscriminator(node);
|
|
904
939
|
const groupIdent = groupIdentifiers.get(key);
|
|
905
940
|
if (groupIdent !== void 0) {
|
|
906
941
|
const local = Object.fromEntries([...Object.entries(node), ["$ref", `#/$defs/${groupIdent}Schema`]]);
|
|
@@ -1005,8 +1040,8 @@ function makeSchemasCode(components, adapter, options) {
|
|
|
1005
1040
|
if (!schemas) return "";
|
|
1006
1041
|
const declarations = makeSchemaDeclarations(schemas, adapter, options);
|
|
1007
1042
|
if (declarations.length === 0) return "";
|
|
1008
|
-
const cyclic = declarations.some((
|
|
1009
|
-
const code = declarations.map((
|
|
1043
|
+
const cyclic = declarations.some((declaration) => declaration.importLine !== schemaImportLine(adapter, options, false, declaration.code));
|
|
1044
|
+
const code = declarations.map((declaration) => declaration.code).join(";");
|
|
1010
1045
|
return `${schemaImportLine(adapter, options, cyclic, code)}\n\n${code}\n`;
|
|
1011
1046
|
}
|
|
1012
1047
|
// src/generator/components/security-schemes.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oas-truth",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "oas-truth is a parser from OpenAPI and TypeSpec to a typed OpenAPI document",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openapi",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"provenance": true
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"schema-truth": "^0.8.
|
|
35
|
+
"schema-truth": "^0.8.15"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@apidevtools/swagger-parser": "^13.1.0",
|