@distilled.cloud/core 1.0.0-rc.12 → 1.0.0-rc.14
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/lib/api.d.ts +4 -4
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +3 -5
- package/lib/api.js.map +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +3 -5
- package/lib/category.js.map +1 -1
- package/lib/codegen/boolean-string-enums.d.ts +8 -8
- package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
- package/lib/codegen/boolean-string-enums.js +83 -30
- package/lib/codegen/boolean-string-enums.js.map +1 -1
- package/lib/codegen/boolean-string-enums.test.js +130 -2
- package/lib/codegen/boolean-string-enums.test.js.map +1 -1
- package/lib/codegen/cli.d.ts +5 -4
- package/lib/codegen/cli.d.ts.map +1 -1
- package/lib/codegen/cli.js +12 -9
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/defer-naming.test.d.ts +2 -0
- package/lib/codegen/defer-naming.test.d.ts.map +1 -0
- package/lib/codegen/defer-naming.test.js +105 -0
- package/lib/codegen/defer-naming.test.js.map +1 -0
- package/lib/codegen/emit.d.ts.map +1 -1
- package/lib/codegen/emit.js +1 -3
- package/lib/codegen/emit.js.map +1 -1
- package/lib/codegen/format.d.ts +0 -12
- package/lib/codegen/format.d.ts.map +1 -1
- package/lib/codegen/format.js +10 -6
- package/lib/codegen/format.js.map +1 -1
- package/lib/codegen/generated-at.d.ts +29 -0
- package/lib/codegen/generated-at.d.ts.map +1 -0
- package/lib/codegen/generated-at.js +78 -0
- package/lib/codegen/generated-at.js.map +1 -0
- package/lib/codegen/generated-at.test.d.ts +2 -0
- package/lib/codegen/generated-at.test.d.ts.map +1 -0
- package/lib/codegen/generated-at.test.js +81 -0
- package/lib/codegen/generated-at.test.js.map +1 -0
- package/lib/codegen/generator.d.ts +24 -5
- package/lib/codegen/generator.d.ts.map +1 -1
- package/lib/codegen/generator.js +69 -80
- package/lib/codegen/generator.js.map +1 -1
- package/lib/codegen/generator.test.d.ts +2 -0
- package/lib/codegen/generator.test.d.ts.map +1 -0
- package/lib/codegen/generator.test.js +121 -0
- package/lib/codegen/generator.test.js.map +1 -0
- package/lib/codegen/graphql-client.d.ts +7 -1
- package/lib/codegen/graphql-client.d.ts.map +1 -1
- package/lib/codegen/graphql-client.js +264 -148
- package/lib/codegen/graphql-client.js.map +1 -1
- package/lib/codegen/graphql-client.test.js +98 -238
- package/lib/codegen/graphql-client.test.js.map +1 -1
- package/lib/codegen/graphql.d.ts.map +1 -1
- package/lib/codegen/graphql.js +7 -20
- package/lib/codegen/graphql.js.map +1 -1
- package/lib/codegen/model-diff.d.ts +10 -0
- package/lib/codegen/model-diff.d.ts.map +1 -0
- package/lib/codegen/model-diff.js +67 -0
- package/lib/codegen/model-diff.js.map +1 -0
- package/lib/codegen/naming.d.ts.map +1 -1
- package/lib/codegen/naming.js.map +1 -1
- package/lib/codegen/openapi-binary.test.d.ts +2 -0
- package/lib/codegen/openapi-binary.test.d.ts.map +1 -0
- package/lib/codegen/openapi-binary.test.js +67 -0
- package/lib/codegen/openapi-binary.test.js.map +1 -0
- package/lib/codegen/openapi-cli.d.ts +11 -11
- package/lib/codegen/openapi-cli.d.ts.map +1 -1
- package/lib/codegen/openapi-cli.js +26 -51
- package/lib/codegen/openapi-cli.js.map +1 -1
- package/lib/codegen/openapi.d.ts +16 -2
- package/lib/codegen/openapi.d.ts.map +1 -1
- package/lib/codegen/openapi.js +56 -39
- package/lib/codegen/openapi.js.map +1 -1
- package/lib/codegen/operations.d.ts.map +1 -1
- package/lib/codegen/operations.js.map +1 -1
- package/lib/codegen/pagination.d.ts.map +1 -1
- package/lib/codegen/pagination.js +2 -4
- package/lib/codegen/pagination.js.map +1 -1
- package/lib/codegen/patch-audit.d.ts +84 -0
- package/lib/codegen/patch-audit.d.ts.map +1 -0
- package/lib/codegen/patch-audit.js +366 -0
- package/lib/codegen/patch-audit.js.map +1 -0
- package/lib/codegen/patch-audit.test.d.ts +2 -0
- package/lib/codegen/patch-audit.test.d.ts.map +1 -0
- package/lib/codegen/patch-audit.test.js +141 -0
- package/lib/codegen/patch-audit.test.js.map +1 -0
- package/lib/codegen/patches.d.ts +75 -11
- package/lib/codegen/patches.d.ts.map +1 -1
- package/lib/codegen/patches.js +301 -65
- package/lib/codegen/patches.js.map +1 -1
- package/lib/codegen/patches.test.js +55 -5
- package/lib/codegen/patches.test.js.map +1 -1
- package/lib/codegen/prelude.js +1 -1
- package/lib/codegen/prelude.js.map +1 -1
- package/lib/codegen/proto.d.ts.map +1 -1
- package/lib/codegen/proto.js +2 -4
- package/lib/codegen/proto.js.map +1 -1
- package/lib/codegen/protocol-imports.test.d.ts +2 -0
- package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
- package/lib/codegen/protocol-imports.test.js +36 -0
- package/lib/codegen/protocol-imports.test.js.map +1 -0
- package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
- package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
- package/lib/codegen/rewrite-operation-ids.js +97 -59
- package/lib/codegen/rewrite-operation-ids.js.map +1 -1
- package/lib/codegen/rewrite-operation-ids.test.js +19 -101
- package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
- package/lib/codegen/spec-path.d.ts +1 -1
- package/lib/codegen/spec-path.d.ts.map +1 -1
- package/lib/codegen/spec-path.js +1 -1
- package/lib/codegen/spec-path.js.map +1 -1
- package/lib/codegen/untagged-unions.test.d.ts +2 -0
- package/lib/codegen/untagged-unions.test.d.ts.map +1 -0
- package/lib/codegen/untagged-unions.test.js +80 -0
- package/lib/codegen/untagged-unions.test.js.map +1 -0
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js.map +1 -1
- package/lib/errors.d.ts +2 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +9 -4
- package/lib/errors.js.map +1 -1
- package/lib/fixtures/xml/upstream-validator.json +397 -0
- package/lib/graphql.d.ts +292 -205
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +945 -636
- package/lib/graphql.js.map +1 -1
- package/lib/graphql.test.js +135 -764
- package/lib/graphql.test.js.map +1 -1
- package/lib/graphql.types.d.ts +10 -0
- package/lib/graphql.types.d.ts.map +1 -1
- package/lib/graphql.types.js +4 -44
- package/lib/graphql.types.js.map +1 -1
- package/lib/index.d.ts +27 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +27 -0
- package/lib/index.js.map +1 -0
- package/lib/json-patch.d.ts +13 -6
- package/lib/json-patch.d.ts.map +1 -1
- package/lib/json-patch.js +34 -19
- package/lib/json-patch.js.map +1 -1
- package/lib/json-patch.test.d.ts +2 -0
- package/lib/json-patch.test.d.ts.map +1 -0
- package/lib/json-patch.test.js +65 -0
- package/lib/json-patch.test.js.map +1 -0
- package/lib/pagination.d.ts +2 -1
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +28 -30
- package/lib/pagination.js.map +1 -1
- package/lib/pagination.test.d.ts +2 -0
- package/lib/pagination.test.d.ts.map +1 -0
- package/lib/pagination.test.js +131 -0
- package/lib/pagination.test.js.map +1 -0
- package/lib/protocol-http.d.ts +30 -16
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +133 -84
- package/lib/protocol-http.js.map +1 -1
- package/lib/protocol-http.test.js +141 -1
- package/lib/protocol-http.test.js.map +1 -1
- package/lib/protocol-rest.d.ts +18 -1
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +32 -21
- package/lib/protocol-rest.js.map +1 -1
- package/lib/protocol-rest.test.d.ts +2 -0
- package/lib/protocol-rest.test.d.ts.map +1 -0
- package/lib/protocol-rest.test.js +498 -0
- package/lib/protocol-rest.test.js.map +1 -0
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +27 -0
- package/lib/query.js.map +1 -0
- package/lib/response-validation.d.ts +67 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +76 -0
- package/lib/response-validation.js.map +1 -0
- package/lib/response-validation.test.d.ts +2 -0
- package/lib/response-validation.test.d.ts.map +1 -0
- package/lib/response-validation.test.js +72 -0
- package/lib/response-validation.test.js.map +1 -0
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -1
- package/lib/retry-after.js.map +1 -1
- package/lib/retry-after.test.d.ts +2 -0
- package/lib/retry-after.test.d.ts.map +1 -0
- package/lib/retry-after.test.js +204 -0
- package/lib/retry-after.test.js.map +1 -0
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -2
- package/lib/retry.js.map +1 -1
- package/lib/retry.test.d.ts +2 -0
- package/lib/retry.test.d.ts.map +1 -0
- package/lib/retry.test.js +407 -0
- package/lib/retry.test.js.map +1 -0
- package/lib/sdks.test.d.ts +2 -0
- package/lib/sdks.test.d.ts.map +1 -0
- package/lib/sdks.test.js +76 -0
- package/lib/sdks.test.js.map +1 -0
- package/lib/testing.d.ts +39 -0
- package/lib/testing.d.ts.map +1 -0
- package/lib/testing.js +41 -0
- package/lib/testing.js.map +1 -0
- package/lib/trait.d.ts +29 -9
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +8 -0
- package/lib/trait.js.map +1 -1
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +311 -0
- package/lib/xml.js.map +1 -0
- package/lib/xml.test.d.ts +2 -0
- package/lib/xml.test.d.ts.map +1 -0
- package/lib/xml.test.js +210 -0
- package/lib/xml.test.js.map +1 -0
- package/package.json +8 -11
- package/src/api.ts +38 -55
- package/src/category.ts +21 -50
- package/src/codegen/boolean-string-enums.test.ts +148 -17
- package/src/codegen/boolean-string-enums.ts +81 -32
- package/src/codegen/cli.ts +29 -47
- package/src/codegen/defer-naming.test.ts +113 -0
- package/src/codegen/emit.ts +2 -5
- package/src/codegen/format.ts +12 -10
- package/src/codegen/generated-at.test.ts +89 -0
- package/src/codegen/generated-at.ts +88 -0
- package/src/codegen/generator.test.ts +137 -0
- package/src/codegen/generator.ts +132 -193
- package/src/codegen/graphql-client.test.ts +100 -297
- package/src/codegen/graphql-client.ts +302 -210
- package/src/codegen/graphql.ts +22 -94
- package/src/codegen/model-diff.ts +67 -0
- package/src/codegen/naming.ts +1 -2
- package/src/codegen/openapi-binary.test.ts +91 -0
- package/src/codegen/openapi-cli.ts +38 -79
- package/src/codegen/openapi.ts +98 -133
- package/src/codegen/operations.ts +2 -5
- package/src/codegen/pagination.ts +4 -11
- package/src/codegen/patch-audit.test.ts +160 -0
- package/src/codegen/patch-audit.ts +480 -0
- package/src/codegen/patches.test.ts +73 -15
- package/src/codegen/patches.ts +389 -99
- package/src/codegen/prelude.ts +1 -1
- package/src/codegen/proto.ts +18 -54
- package/src/codegen/protocol-imports.test.ts +39 -0
- package/src/codegen/rewrite-operation-ids.test.ts +23 -114
- package/src/codegen/rewrite-operation-ids.ts +111 -98
- package/src/codegen/spec-path.ts +3 -6
- package/src/codegen/untagged-unions.test.ts +85 -0
- package/src/error-category.ts +5 -17
- package/src/errors.ts +18 -30
- package/src/fixtures/xml/upstream-validator.json +397 -0
- package/src/graphql.test.ts +173 -949
- package/src/graphql.ts +1468 -1186
- package/src/graphql.types.ts +78 -164
- package/src/index.ts +26 -0
- package/src/json-patch.test.ts +77 -0
- package/src/json-patch.ts +54 -60
- package/src/pagination.test.ts +169 -0
- package/src/pagination.ts +46 -103
- package/src/protocol-http.test.ts +178 -9
- package/src/protocol-http.ts +174 -163
- package/src/protocol-rest.test.ts +703 -0
- package/src/protocol-rest.ts +76 -65
- package/src/query.ts +39 -0
- package/src/response-validation.test.ts +89 -0
- package/src/response-validation.ts +107 -0
- package/src/retry-after.test.ts +267 -0
- package/src/retry-after.ts +4 -10
- package/src/retry.test.ts +566 -0
- package/src/retry.ts +6 -21
- package/src/sdks.test.ts +87 -0
- package/src/testing.ts +72 -0
- package/src/trait.ts +48 -63
- package/src/xml.test.ts +251 -0
- package/src/xml.ts +323 -0
- package/lib/graphql.fixture.d.ts +0 -249
- package/lib/graphql.fixture.d.ts.map +0 -1
- package/lib/graphql.fixture.js +0 -240
- package/lib/graphql.fixture.js.map +0 -1
- package/src/graphql.fixture.ts +0 -371
package/src/codegen/openapi.ts
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* across operations; anonymous nested objects synthesize names from the
|
|
24
24
|
* parent + member path
|
|
25
25
|
* • responses: 200 → 201 → 204 precedence (`successStatuses` overrides),
|
|
26
|
-
* `application/
|
|
26
|
+
* JSON or opt-in binary `application/octet-stream`; object
|
|
27
27
|
* results become `<Op>Response` structures (a sole `$ref` reuses the named
|
|
28
28
|
* shape); bare array/scalar results wrap in a structure whose single
|
|
29
29
|
* member carries `com.distilled.openapi#rawResponse` (the SdkSpec maps it
|
|
@@ -48,6 +48,8 @@ import {
|
|
|
48
48
|
isMechanicalOperationId,
|
|
49
49
|
isVerbatimRouteId,
|
|
50
50
|
pathToVerbNoun,
|
|
51
|
+
RENAME_METADATA_KEY,
|
|
52
|
+
renameShapes,
|
|
51
53
|
resolveOperationName,
|
|
52
54
|
toVerbNoun,
|
|
53
55
|
type OperationIdRewrite,
|
|
@@ -161,6 +163,12 @@ export interface OpenApiConvertOptions {
|
|
|
161
163
|
* ask for them.
|
|
162
164
|
*/
|
|
163
165
|
readonly headerParams?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Convert `format: binary` strings to blobs and include binary
|
|
168
|
+
* `application/octet-stream` responses. Opt in only when the provider's
|
|
169
|
+
* generator and protocol support binary inputs and outputs. Default false.
|
|
170
|
+
*/
|
|
171
|
+
readonly binaryTypes?: boolean;
|
|
164
172
|
/**
|
|
165
173
|
* Response statuses to read the operation's output shape from, most
|
|
166
174
|
* preferred first. Default `["200", "201", "204"]`. Extend it for an API
|
|
@@ -211,6 +219,13 @@ export interface OpenApiConvertOptions {
|
|
|
211
219
|
* `"METHOD path"` keys when two methods share an `operationId`.
|
|
212
220
|
*/
|
|
213
221
|
readonly operationNames?: OperationIdRewrite;
|
|
222
|
+
/**
|
|
223
|
+
* Name shapes from the spec (`PascalCase(operationId)`, no overrides) and
|
|
224
|
+
* record the {@link operationNaming} names in
|
|
225
|
+
* `metadata[RENAME_METADATA_KEY]`; `finalizeConvert` renames after the
|
|
226
|
+
* Smithy patches, so patches target upstream names.
|
|
227
|
+
*/
|
|
228
|
+
readonly deferNaming?: boolean;
|
|
214
229
|
}
|
|
215
230
|
|
|
216
231
|
export interface SmithyModel {
|
|
@@ -226,6 +241,7 @@ export interface SmithyModel {
|
|
|
226
241
|
const PRELUDE = {
|
|
227
242
|
Unit: "smithy.api#Unit",
|
|
228
243
|
String: "smithy.api#String",
|
|
244
|
+
Blob: "smithy.api#Blob",
|
|
229
245
|
Boolean: "smithy.api#Boolean",
|
|
230
246
|
Double: "smithy.api#Double",
|
|
231
247
|
Integer: "smithy.api#Integer",
|
|
@@ -258,9 +274,7 @@ const headerMemberName = (name: string): string => {
|
|
|
258
274
|
if (parts.length === 0) return "_";
|
|
259
275
|
const out = parts
|
|
260
276
|
.map((p, i) =>
|
|
261
|
-
i === 0
|
|
262
|
-
? p.charAt(0).toLowerCase() + p.slice(1)
|
|
263
|
-
: p.charAt(0).toUpperCase() + p.slice(1),
|
|
277
|
+
i === 0 ? p.charAt(0).toLowerCase() + p.slice(1) : p.charAt(0).toUpperCase() + p.slice(1),
|
|
264
278
|
)
|
|
265
279
|
.join("");
|
|
266
280
|
return /^[0-9]/.test(out) ? `_${out}` : out;
|
|
@@ -308,6 +322,7 @@ interface Ctx {
|
|
|
308
322
|
*/
|
|
309
323
|
readonly dirSensitiveRefs: Map<Dir, ReadonlySet<string>>;
|
|
310
324
|
readonly sensitivePatterns: readonly RegExp[];
|
|
325
|
+
readonly binaryTypes: boolean;
|
|
311
326
|
}
|
|
312
327
|
|
|
313
328
|
interface Converted {
|
|
@@ -392,12 +407,7 @@ const flattenObject = (ctx: Ctx, def: any, depth = 0): FlatObject => {
|
|
|
392
407
|
// once per flag combination instead of exponentially (MongoDB Atlas's
|
|
393
408
|
// polymorphic allOf/oneOf graph never finished under the bare depth cap).
|
|
394
409
|
const seen = new Set<string>();
|
|
395
|
-
const visit = (
|
|
396
|
-
d: any,
|
|
397
|
-
dep: number,
|
|
398
|
-
inAllOf: boolean,
|
|
399
|
-
inUnion: boolean,
|
|
400
|
-
): void => {
|
|
410
|
+
const visit = (d: any, dep: number, inAllOf: boolean, inUnion: boolean): void => {
|
|
401
411
|
if (dep > MAX_SCHEMA_DEPTH) return;
|
|
402
412
|
if (d && typeof d === "object" && typeof d.$ref === "string") {
|
|
403
413
|
const key = `${d.$ref}|${inAllOf}|${inUnion}`;
|
|
@@ -447,11 +457,7 @@ const isNullBranch = (ctx: Ctx, branch: any): boolean => {
|
|
|
447
457
|
if (Array.isArray(r.type) && r.type.every((t: unknown) => t === "null")) {
|
|
448
458
|
return true;
|
|
449
459
|
}
|
|
450
|
-
if (
|
|
451
|
-
Array.isArray(r.enum) &&
|
|
452
|
-
r.enum.length > 0 &&
|
|
453
|
-
r.enum.every((v: unknown) => v === null)
|
|
454
|
-
) {
|
|
460
|
+
if (Array.isArray(r.enum) && r.enum.length > 0 && r.enum.every((v: unknown) => v === null)) {
|
|
455
461
|
return true;
|
|
456
462
|
}
|
|
457
463
|
return false;
|
|
@@ -487,9 +493,7 @@ const schemaChildren = (d: any): any[] => [
|
|
|
487
493
|
...(Array.isArray(d.allOf) ? d.allOf : []),
|
|
488
494
|
...(Array.isArray(d.oneOf) ? d.oneOf : []),
|
|
489
495
|
...(Array.isArray(d.anyOf) ? d.anyOf : []),
|
|
490
|
-
...(d.properties && typeof d.properties === "object"
|
|
491
|
-
? Object.values(d.properties)
|
|
492
|
-
: []),
|
|
496
|
+
...(d.properties && typeof d.properties === "object" ? Object.values(d.properties) : []),
|
|
493
497
|
...(d.items ? [d.items] : []),
|
|
494
498
|
...(d.additionalProperties && typeof d.additionalProperties === "object"
|
|
495
499
|
? [d.additionalProperties]
|
|
@@ -755,14 +759,7 @@ const convertSchema = (
|
|
|
755
759
|
// Single-entry allOf over a $ref: passthrough (v0 semantics), carrying
|
|
756
760
|
// the parent's nullability/description.
|
|
757
761
|
if (def.allOf.length === 1 && !def.properties) {
|
|
758
|
-
const r = convertSchema(
|
|
759
|
-
ctx,
|
|
760
|
-
def.allOf[0],
|
|
761
|
-
hint,
|
|
762
|
-
depth + 1,
|
|
763
|
-
dir,
|
|
764
|
-
reservedId,
|
|
765
|
-
);
|
|
762
|
+
const r = convertSchema(ctx, def.allOf[0], hint, depth + 1, dir, reservedId);
|
|
766
763
|
return { target: r.target, nullable: nullable || r.nullable };
|
|
767
764
|
}
|
|
768
765
|
const flat = flattenObject(ctx, def, depth);
|
|
@@ -782,14 +779,7 @@ const convertSchema = (
|
|
|
782
779
|
}
|
|
783
780
|
if (!flat.isObject) return inline(PRELUDE.Document, nullable);
|
|
784
781
|
}
|
|
785
|
-
const members = buildMembers(
|
|
786
|
-
ctx,
|
|
787
|
-
flat.properties,
|
|
788
|
-
flat.required,
|
|
789
|
-
hint,
|
|
790
|
-
depth + 1,
|
|
791
|
-
dir,
|
|
792
|
-
);
|
|
782
|
+
const members = buildMembers(ctx, flat.properties, flat.required, hint, depth + 1, dir);
|
|
793
783
|
return {
|
|
794
784
|
target: emit({ type: "structure", members, traits: docTraits }, hint),
|
|
795
785
|
nullable,
|
|
@@ -869,17 +859,8 @@ const convertSchema = (
|
|
|
869
859
|
// --- object ---------------------------------------------------------------
|
|
870
860
|
if (t === "object" || def.properties || def.additionalProperties) {
|
|
871
861
|
if (def.properties && Object.keys(def.properties).length > 0) {
|
|
872
|
-
const required = new Set<string>(
|
|
873
|
-
|
|
874
|
-
);
|
|
875
|
-
const members = buildMembers(
|
|
876
|
-
ctx,
|
|
877
|
-
def.properties,
|
|
878
|
-
required,
|
|
879
|
-
hint,
|
|
880
|
-
depth + 1,
|
|
881
|
-
dir,
|
|
882
|
-
);
|
|
862
|
+
const required = new Set<string>(Array.isArray(def.required) ? def.required : []);
|
|
863
|
+
const members = buildMembers(ctx, def.properties, required, hint, depth + 1, dir);
|
|
883
864
|
return {
|
|
884
865
|
target: emit({ type: "structure", members, traits: docTraits }, hint),
|
|
885
866
|
nullable,
|
|
@@ -913,7 +894,10 @@ const convertSchema = (
|
|
|
913
894
|
// --- scalars --------------------------------------------------------------
|
|
914
895
|
switch (t) {
|
|
915
896
|
case "string":
|
|
916
|
-
return inline(
|
|
897
|
+
return inline(
|
|
898
|
+
ctx.binaryTypes && def.format === "binary" ? PRELUDE.Blob : PRELUDE.String,
|
|
899
|
+
nullable,
|
|
900
|
+
);
|
|
917
901
|
case "boolean":
|
|
918
902
|
return inline(PRELUDE.Boolean, nullable);
|
|
919
903
|
case "integer":
|
|
@@ -1036,11 +1020,7 @@ const collectParams = (ctx: Ctx, pathItem: any, op: any): Param[] => {
|
|
|
1036
1020
|
byKey.set(`${p.in}${p.name}`, p);
|
|
1037
1021
|
}
|
|
1038
1022
|
return [...byKey.values()].filter(
|
|
1039
|
-
(p) =>
|
|
1040
|
-
p.in === "path" ||
|
|
1041
|
-
p.in === "query" ||
|
|
1042
|
-
p.in === "body" ||
|
|
1043
|
-
p.in === "header",
|
|
1023
|
+
(p) => p.in === "path" || p.in === "query" || p.in === "body" || p.in === "header",
|
|
1044
1024
|
);
|
|
1045
1025
|
};
|
|
1046
1026
|
|
|
@@ -1091,12 +1071,9 @@ const trimDescription = (desc: unknown): string | undefined => {
|
|
|
1091
1071
|
};
|
|
1092
1072
|
|
|
1093
1073
|
const opDoc = (op: any): string | undefined => {
|
|
1094
|
-
const summary =
|
|
1095
|
-
typeof op.summary === "string" ? op.summary.trim() : undefined;
|
|
1074
|
+
const summary = typeof op.summary === "string" ? op.summary.trim() : undefined;
|
|
1096
1075
|
const desc = trimDescription(op.description);
|
|
1097
|
-
const parts = [summary, desc].filter(
|
|
1098
|
-
(s): s is string => s !== undefined && s !== "",
|
|
1099
|
-
);
|
|
1076
|
+
const parts = [summary, desc].filter((s): s is string => s !== undefined && s !== "");
|
|
1100
1077
|
return parts.length ? parts.join("\n\n") : undefined;
|
|
1101
1078
|
};
|
|
1102
1079
|
|
|
@@ -1104,7 +1081,7 @@ const opDoc = (op: any): string | undefined => {
|
|
|
1104
1081
|
// Responses
|
|
1105
1082
|
// ============================================================================
|
|
1106
1083
|
|
|
1107
|
-
/** First declared status
|
|
1084
|
+
/** First declared status wins; resolve response refs and preserve JSON or binary bodies. */
|
|
1108
1085
|
const successSchema = (
|
|
1109
1086
|
ctx: Ctx,
|
|
1110
1087
|
responses: any,
|
|
@@ -1118,7 +1095,12 @@ const successSchema = (
|
|
|
1118
1095
|
if (ctx.version === "2.0") {
|
|
1119
1096
|
return { schema: resp.schema };
|
|
1120
1097
|
}
|
|
1121
|
-
|
|
1098
|
+
const json = resp.content?.["application/json"]?.schema;
|
|
1099
|
+
const binary = resp.content?.["application/octet-stream"]?.schema;
|
|
1100
|
+
return {
|
|
1101
|
+
schema:
|
|
1102
|
+
json ?? (ctx.binaryTypes && deref(ctx, binary)?.format === "binary" ? binary : undefined),
|
|
1103
|
+
};
|
|
1122
1104
|
}
|
|
1123
1105
|
return { schema: undefined };
|
|
1124
1106
|
};
|
|
@@ -1182,9 +1164,7 @@ const detectPagination = (
|
|
|
1182
1164
|
|
|
1183
1165
|
let mode: DetectedPagination["mode"] | undefined;
|
|
1184
1166
|
let outputToken: string | undefined;
|
|
1185
|
-
const pag = bag["pagination"]
|
|
1186
|
-
? flattenObject(ctx, bag["pagination"]).properties
|
|
1187
|
-
: undefined;
|
|
1167
|
+
const pag = bag["pagination"] ? flattenObject(ctx, bag["pagination"]).properties : undefined;
|
|
1188
1168
|
if (pag?.["cursor"]) {
|
|
1189
1169
|
mode = "cursor";
|
|
1190
1170
|
outputToken = "pagination.cursor";
|
|
@@ -1210,9 +1190,7 @@ const detectPagination = (
|
|
|
1210
1190
|
if (!mode || !outputToken) return undefined;
|
|
1211
1191
|
|
|
1212
1192
|
const aliases = PAGINATION_INPUT_ALIASES[mode]!;
|
|
1213
|
-
const inputToken = params.find(
|
|
1214
|
-
(p) => p.in === "query" && aliases.includes(p.name),
|
|
1215
|
-
)?.name;
|
|
1193
|
+
const inputToken = params.find((p) => p.in === "query" && aliases.includes(p.name))?.name;
|
|
1216
1194
|
if (!inputToken) return undefined;
|
|
1217
1195
|
|
|
1218
1196
|
let items: string | undefined;
|
|
@@ -1254,10 +1232,48 @@ const DEFAULT_ERROR_STATUSES = ["401", "429", "500", "503"];
|
|
|
1254
1232
|
|
|
1255
1233
|
const DEFAULT_SUCCESS_STATUSES = ["200", "201", "204"];
|
|
1256
1234
|
|
|
1235
|
+
export { RENAME_METADATA_KEY };
|
|
1236
|
+
|
|
1257
1237
|
export const convertOpenApiToSmithy = (
|
|
1258
1238
|
spec: unknown,
|
|
1259
1239
|
options: OpenApiConvertOptions,
|
|
1260
1240
|
): SmithyModel => {
|
|
1241
|
+
const named = convertOnce(spec, options);
|
|
1242
|
+
if (!options.deferNaming) return named;
|
|
1243
|
+
const upstream = convertOnce(spec, {
|
|
1244
|
+
...options,
|
|
1245
|
+
operationNaming: "as-is",
|
|
1246
|
+
operationNames: undefined,
|
|
1247
|
+
});
|
|
1248
|
+
// Names never steer the conversion (only uniqueness suffixes and the
|
|
1249
|
+
// `By<Param>` rule read them), so both runs create the same shapes in the
|
|
1250
|
+
// same order: the i-th shape of one is the i-th of the other.
|
|
1251
|
+
const from = Object.keys(upstream.shapes);
|
|
1252
|
+
const to = Object.keys(named.shapes);
|
|
1253
|
+
if (from.length !== to.length) {
|
|
1254
|
+
throw new Error(
|
|
1255
|
+
`deferNaming (${options.namespace}): ${from.length} shapes with upstream names, ${to.length} with final names`,
|
|
1256
|
+
);
|
|
1257
|
+
}
|
|
1258
|
+
const rename: Record<string, string> = {};
|
|
1259
|
+
for (let i = 0; i < from.length; i++) if (from[i] !== to[i]) rename[from[i]!] = to[i]!;
|
|
1260
|
+
const check = structuredClone(upstream);
|
|
1261
|
+
renameShapes(check, new Map(Object.entries(rename)));
|
|
1262
|
+
if (JSON.stringify(check) !== JSON.stringify(named)) {
|
|
1263
|
+
const differs = to.find(
|
|
1264
|
+
(id) => JSON.stringify(check.shapes[id]) !== JSON.stringify(named.shapes[id]),
|
|
1265
|
+
);
|
|
1266
|
+
throw new Error(
|
|
1267
|
+
`deferNaming (${options.namespace}): the upstream-named model does not rename to the final one (first difference: ${differs ?? "metadata"})`,
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
if (Object.keys(rename).length > 0) {
|
|
1271
|
+
upstream.metadata = { ...upstream.metadata, [RENAME_METADATA_KEY]: rename };
|
|
1272
|
+
}
|
|
1273
|
+
return upstream;
|
|
1274
|
+
};
|
|
1275
|
+
|
|
1276
|
+
const convertOnce = (spec: unknown, options: OpenApiConvertOptions): SmithyModel => {
|
|
1261
1277
|
const doc = spec as any;
|
|
1262
1278
|
const version = detectVersion(doc);
|
|
1263
1279
|
const ctx: Ctx = {
|
|
@@ -1269,19 +1285,15 @@ export const convertOpenApiToSmithy = (
|
|
|
1269
1285
|
refs: new Map(),
|
|
1270
1286
|
dirSensitiveRefs: new Map(),
|
|
1271
1287
|
sensitivePatterns: options.sensitivePatterns ?? SENSITIVE_FIELD_PATTERNS,
|
|
1288
|
+
binaryTypes: options.binaryTypes ?? false,
|
|
1272
1289
|
};
|
|
1273
|
-
const statusToErrorClass =
|
|
1274
|
-
|
|
1275
|
-
const defaultErrorStatuses = new Set(
|
|
1276
|
-
options.defaultErrorStatuses ?? DEFAULT_ERROR_STATUSES,
|
|
1277
|
-
);
|
|
1290
|
+
const statusToErrorClass = options.statusToErrorClass ?? DEFAULT_STATUS_TO_ERROR_CLASS;
|
|
1291
|
+
const defaultErrorStatuses = new Set(options.defaultErrorStatuses ?? DEFAULT_ERROR_STATUSES);
|
|
1278
1292
|
const skipDeprecated = options.skipDeprecated ?? true;
|
|
1279
1293
|
const successStatuses = options.successStatuses ?? DEFAULT_SUCCESS_STATUSES;
|
|
1280
1294
|
const httpMethods = [
|
|
1281
1295
|
...HTTP_METHODS,
|
|
1282
|
-
...OPTIONAL_HTTP_METHODS.filter((m) =>
|
|
1283
|
-
options.extraHttpMethods?.includes(m),
|
|
1284
|
-
),
|
|
1296
|
+
...OPTIONAL_HTTP_METHODS.filter((m) => options.extraHttpMethods?.includes(m)),
|
|
1285
1297
|
];
|
|
1286
1298
|
|
|
1287
1299
|
// Error class names and the service name are reserved up front so schema
|
|
@@ -1321,11 +1333,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1321
1333
|
// No id: everything comes from the route, including whether a
|
|
1322
1334
|
// GET on a collection route is `list` or `get`.
|
|
1323
1335
|
named = pathToVerbNoun(idCtx, {
|
|
1324
|
-
returnsCollection: responseIsCollection(
|
|
1325
|
-
ctx,
|
|
1326
|
-
op.responses,
|
|
1327
|
-
successStatuses,
|
|
1328
|
-
),
|
|
1336
|
+
returnsCollection: responseIsCollection(ctx, op.responses, successStatuses),
|
|
1329
1337
|
});
|
|
1330
1338
|
} else if (isMechanicalOperationId(op.operationId, idCtx)) {
|
|
1331
1339
|
// Method-prefixed and reading the route (`get-feeds`,
|
|
@@ -1361,9 +1369,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1361
1369
|
|
|
1362
1370
|
// ---- URI + labels (sanitize placeholder names to member idents) ----
|
|
1363
1371
|
let uri = rawPath.split(/[?#]/)[0]!;
|
|
1364
|
-
const rawLabels = Array.from(uri.matchAll(/\{([^}]+)\}/g)).map(
|
|
1365
|
-
(m) => m[1]!,
|
|
1366
|
-
);
|
|
1372
|
+
const rawLabels = Array.from(uri.matchAll(/\{([^}]+)\}/g)).map((m) => m[1]!);
|
|
1367
1373
|
const members: Record<string, any> = {};
|
|
1368
1374
|
const addMember = (name: string, member: any): boolean => {
|
|
1369
1375
|
if (name in members) return false;
|
|
@@ -1380,9 +1386,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1380
1386
|
traits: {
|
|
1381
1387
|
"smithy.api#httpLabel": {},
|
|
1382
1388
|
"smithy.api#required": {},
|
|
1383
|
-
...(p?.description
|
|
1384
|
-
? { "smithy.api#documentation": p.description }
|
|
1385
|
-
: {}),
|
|
1389
|
+
...(p?.description ? { "smithy.api#documentation": p.description } : {}),
|
|
1386
1390
|
},
|
|
1387
1391
|
});
|
|
1388
1392
|
}
|
|
@@ -1394,21 +1398,13 @@ export const convertOpenApiToSmithy = (
|
|
|
1394
1398
|
continue;
|
|
1395
1399
|
}
|
|
1396
1400
|
const san = memberIdent(p.name);
|
|
1397
|
-
const conv = convertSchema(
|
|
1398
|
-
ctx,
|
|
1399
|
-
p.schema,
|
|
1400
|
-
`${opName}Request${pascal(p.name)}`,
|
|
1401
|
-
0,
|
|
1402
|
-
"in",
|
|
1403
|
-
);
|
|
1401
|
+
const conv = convertSchema(ctx, p.schema, `${opName}Request${pascal(p.name)}`, 0, "in");
|
|
1404
1402
|
addMember(san, {
|
|
1405
1403
|
target: conv.target,
|
|
1406
1404
|
traits: {
|
|
1407
1405
|
"smithy.api#httpQuery": p.name,
|
|
1408
1406
|
...(p.required ? { "smithy.api#required": {} } : {}),
|
|
1409
|
-
...(p.description
|
|
1410
|
-
? { "smithy.api#documentation": p.description }
|
|
1411
|
-
: {}),
|
|
1407
|
+
...(p.description ? { "smithy.api#documentation": p.description } : {}),
|
|
1412
1408
|
},
|
|
1413
1409
|
});
|
|
1414
1410
|
}
|
|
@@ -1417,13 +1413,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1417
1413
|
if (options.headerParams) {
|
|
1418
1414
|
for (const p of params) {
|
|
1419
1415
|
if (p.in !== "header") continue;
|
|
1420
|
-
const conv = convertSchema(
|
|
1421
|
-
ctx,
|
|
1422
|
-
p.schema,
|
|
1423
|
-
`${opName}Request${pascal(p.name)}`,
|
|
1424
|
-
0,
|
|
1425
|
-
"in",
|
|
1426
|
-
);
|
|
1416
|
+
const conv = convertSchema(ctx, p.schema, `${opName}Request${pascal(p.name)}`, 0, "in");
|
|
1427
1417
|
addMember(headerMemberName(p.name), {
|
|
1428
1418
|
target: conv.target,
|
|
1429
1419
|
traits: {
|
|
@@ -1431,9 +1421,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1431
1421
|
// a normal identifier.
|
|
1432
1422
|
"smithy.api#httpHeader": p.name,
|
|
1433
1423
|
...(p.required ? { "smithy.api#required": {} } : {}),
|
|
1434
|
-
...(p.description
|
|
1435
|
-
? { "smithy.api#documentation": p.description }
|
|
1436
|
-
: {}),
|
|
1424
|
+
...(p.description ? { "smithy.api#documentation": p.description } : {}),
|
|
1437
1425
|
},
|
|
1438
1426
|
});
|
|
1439
1427
|
}
|
|
@@ -1448,9 +1436,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1448
1436
|
bodySchema = bodyParam?.schema;
|
|
1449
1437
|
bodyRequired = bodyParam?.required === true;
|
|
1450
1438
|
} else if (op.requestBody) {
|
|
1451
|
-
const rb = op.requestBody.$ref
|
|
1452
|
-
? resolvePointer(doc, op.requestBody.$ref)
|
|
1453
|
-
: op.requestBody;
|
|
1439
|
+
const rb = op.requestBody.$ref ? resolvePointer(doc, op.requestBody.$ref) : op.requestBody;
|
|
1454
1440
|
bodyRequired = rb?.required === true;
|
|
1455
1441
|
const content = rb?.content ?? {};
|
|
1456
1442
|
if (content["application/json"]) {
|
|
@@ -1485,13 +1471,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1485
1471
|
// runtime's unknown-key passthrough is the escape hatch, and an
|
|
1486
1472
|
// opaque `body: unknown` payload member would swallow the typed
|
|
1487
1473
|
// surface.
|
|
1488
|
-
const conv = convertSchema(
|
|
1489
|
-
ctx,
|
|
1490
|
-
bodySchema,
|
|
1491
|
-
`${opName}RequestBody`,
|
|
1492
|
-
0,
|
|
1493
|
-
"in",
|
|
1494
|
-
);
|
|
1474
|
+
const conv = convertSchema(ctx, bodySchema, `${opName}RequestBody`, 0, "in");
|
|
1495
1475
|
if (conv.target !== PRELUDE.Document) {
|
|
1496
1476
|
addMember("body", {
|
|
1497
1477
|
target: conv.target,
|
|
@@ -1540,13 +1520,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1540
1520
|
isNameable(ctx, resolved);
|
|
1541
1521
|
if (isPlainRef) {
|
|
1542
1522
|
// Reuse the named component shape as the output directly.
|
|
1543
|
-
outputTarget = convertSchema(
|
|
1544
|
-
ctx,
|
|
1545
|
-
respSchema,
|
|
1546
|
-
opName,
|
|
1547
|
-
0,
|
|
1548
|
-
"out",
|
|
1549
|
-
).target;
|
|
1523
|
+
outputTarget = convertSchema(ctx, respSchema, opName, 0, "out").target;
|
|
1550
1524
|
} else {
|
|
1551
1525
|
outputTarget = addShape(ctx, `${opName}Response`, {
|
|
1552
1526
|
type: "structure",
|
|
@@ -1565,13 +1539,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1565
1539
|
// Non-flattenable response (bare array/scalar/map/union, or an
|
|
1566
1540
|
// opaque object) → wrapper whose sole TYPED member IS the payload;
|
|
1567
1541
|
// the SdkSpec's rootPipe collapses the wrapper.
|
|
1568
|
-
const conv = convertSchema(
|
|
1569
|
-
ctx,
|
|
1570
|
-
respSchema,
|
|
1571
|
-
`${opName}ResponseBody`,
|
|
1572
|
-
0,
|
|
1573
|
-
"out",
|
|
1574
|
-
);
|
|
1542
|
+
const conv = convertSchema(ctx, respSchema, `${opName}ResponseBody`, 0, "out");
|
|
1575
1543
|
if (conv.target !== PRELUDE.Document || deref(ctx, respSchema)) {
|
|
1576
1544
|
outputTarget = addShape(ctx, `${opName}Response`, {
|
|
1577
1545
|
type: "structure",
|
|
@@ -1650,7 +1618,7 @@ export const convertOpenApiToSmithy = (
|
|
|
1650
1618
|
? {
|
|
1651
1619
|
...base,
|
|
1652
1620
|
...override,
|
|
1653
|
-
traits: { ...base.traits, ...
|
|
1621
|
+
traits: { ...base.traits, ...override.traits },
|
|
1654
1622
|
}
|
|
1655
1623
|
: base;
|
|
1656
1624
|
}
|
|
@@ -1659,14 +1627,11 @@ export const convertOpenApiToSmithy = (
|
|
|
1659
1627
|
ctx.shapes[`${ctx.ns}#${serviceName}`] = {
|
|
1660
1628
|
type: "service",
|
|
1661
1629
|
version:
|
|
1662
|
-
options.serviceVersion ??
|
|
1663
|
-
(typeof doc.info?.version === "string" ? doc.info.version : "1.0"),
|
|
1630
|
+
options.serviceVersion ?? (typeof doc.info?.version === "string" ? doc.info.version : "1.0"),
|
|
1664
1631
|
operations: serviceOps,
|
|
1665
1632
|
traits: {
|
|
1666
1633
|
"smithy.api#title":
|
|
1667
|
-
typeof doc.info?.title === "string"
|
|
1668
|
-
? doc.info.title
|
|
1669
|
-
: options.serviceName,
|
|
1634
|
+
typeof doc.info?.title === "string" ? doc.info.title : options.serviceName,
|
|
1670
1635
|
...(typeof doc.info?.description === "string"
|
|
1671
1636
|
? {
|
|
1672
1637
|
"smithy.api#documentation": trimDescription(doc.info.description),
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import type { ShapeMap } from "./graph.ts";
|
|
1
2
|
/**
|
|
2
3
|
* Operation-level smithy helpers for the SDK generators (dev-time only).
|
|
3
4
|
*/
|
|
4
5
|
import { local } from "./naming.ts";
|
|
5
|
-
import type { ShapeMap } from "./graph.ts";
|
|
6
6
|
|
|
7
7
|
export interface OpEntry {
|
|
8
8
|
id: string;
|
|
@@ -57,10 +57,7 @@ export const ensureNamedIo = (
|
|
|
57
57
|
* Every error shape id referenced by the operations' `errors` lists —
|
|
58
58
|
* deduplicated, existing in the model, sorted by local name.
|
|
59
59
|
*/
|
|
60
|
-
export const collectOpErrorIds = (
|
|
61
|
-
operations: readonly OpEntry[],
|
|
62
|
-
shapes: ShapeMap,
|
|
63
|
-
): string[] => {
|
|
60
|
+
export const collectOpErrorIds = (operations: readonly OpEntry[], shapes: ShapeMap): string[] => {
|
|
64
61
|
const errorIds: string[] = [];
|
|
65
62
|
const seen = new Set<string>();
|
|
66
63
|
for (const op of operations) {
|
|
@@ -54,18 +54,11 @@ export interface ValidatedPagination {
|
|
|
54
54
|
* yield nothing. Returns the items-path root for emitters that promote it
|
|
55
55
|
* to required.
|
|
56
56
|
*/
|
|
57
|
-
export const validatePaginated = (
|
|
58
|
-
o
|
|
59
|
-
): ValidatedPagination => {
|
|
60
|
-
const itemsRoot = String(o.trait.items ?? o.itemsFallback ?? "").split(
|
|
61
|
-
".",
|
|
62
|
-
)[0];
|
|
57
|
+
export const validatePaginated = (o: ValidatePaginatedOptions): ValidatedPagination => {
|
|
58
|
+
const itemsRoot = String(o.trait.items ?? o.itemsFallback ?? "").split(".")[0];
|
|
63
59
|
const tokenOk =
|
|
64
60
|
o.trait.mode === "single" ||
|
|
65
|
-
(typeof o.trait.inputToken === "string" &&
|
|
66
|
-
|
|
67
|
-
const itemsOk =
|
|
68
|
-
o.outputNames.has(itemsRoot) ||
|
|
69
|
-
(o.syntheticOutputs?.has(itemsRoot) ?? false);
|
|
61
|
+
(typeof o.trait.inputToken === "string" && o.inputNames.has(o.trait.inputToken));
|
|
62
|
+
const itemsOk = o.outputNames.has(itemsRoot) || (o.syntheticOutputs?.has(itemsRoot) ?? false);
|
|
70
63
|
return { ok: tokenOk && itemsOk, itemsRoot };
|
|
71
64
|
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
mkdirSync,
|
|
4
|
+
mkdtempSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { afterEach, describe, expect, test } from "vitest";
|
|
13
|
+
import { auditPackage } from "./patch-audit.ts";
|
|
14
|
+
|
|
15
|
+
const dirs: string[] = [];
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
const PATCHES_TS = join(import.meta.dirname, "patches.ts");
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A package: `spec.json` lists field names, and every field becomes a
|
|
26
|
+
* member of `x#Thing`. `patches/spec/` edit the spec in the convert script
|
|
27
|
+
* itself (the way Railway patches its GraphQL schema, out of
|
|
28
|
+
* finalizeConvert's reach); `patches/svc/` are Smithy ops applied in
|
|
29
|
+
* finalizeConvert, whose transform drops a member named `dropped`.
|
|
30
|
+
*/
|
|
31
|
+
const convertFixture = (patches: Record<string, unknown[]>): string => {
|
|
32
|
+
const parent = mkdtempSync(join(tmpdir(), "patch-audit-convert-"));
|
|
33
|
+
dirs.push(parent);
|
|
34
|
+
const root = join(parent, "pkg");
|
|
35
|
+
mkdirSync(join(root, "scripts"), { recursive: true });
|
|
36
|
+
writeFileSync(
|
|
37
|
+
join(root, "package.json"),
|
|
38
|
+
JSON.stringify({ name: "fixture", scripts: { convert: "node scripts/convert.ts" } }),
|
|
39
|
+
);
|
|
40
|
+
writeFileSync(join(root, "spec.json"), JSON.stringify({ fields: { a: true } }));
|
|
41
|
+
writeFileSync(
|
|
42
|
+
join(root, "scripts", "convert.ts"),
|
|
43
|
+
`
|
|
44
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
45
|
+
import { join } from "node:path";
|
|
46
|
+
import { applyRfc6902Files, finalizeConvert, isSmithyPatchPath, listRfc6902PatchFiles } from ${JSON.stringify(PATCHES_TS)};
|
|
47
|
+
const root = join(import.meta.dirname, "..");
|
|
48
|
+
const spec = JSON.parse(readFileSync(join(root, "spec.json"), "utf8"));
|
|
49
|
+
const files = await listRfc6902PatchFiles(join(root, "patches", "spec"));
|
|
50
|
+
const applied = await applyRfc6902Files(spec, files, { include: (op) => !isSmithyPatchPath(op.path) });
|
|
51
|
+
if (applied.errors.length) throw new Error(applied.errors.join("\\n"));
|
|
52
|
+
const members = Object.fromEntries(Object.keys(spec.fields).map((k) => [k, { target: "smithy.api#String" }]));
|
|
53
|
+
mkdirSync(join(root, ".generated-specs"), { recursive: true });
|
|
54
|
+
writeFileSync(join(root, ".generated-specs", "svc.json"), JSON.stringify({ smithy: "2.0", shapes: { "x#Thing": { type: "structure", members } } }));
|
|
55
|
+
await finalizeConvert({
|
|
56
|
+
root,
|
|
57
|
+
operationNaming: "as-is",
|
|
58
|
+
transform: (model) => { delete model.shapes["x#Thing"].members.dropped; },
|
|
59
|
+
});
|
|
60
|
+
`,
|
|
61
|
+
);
|
|
62
|
+
for (const [key, ops] of Object.entries(patches)) {
|
|
63
|
+
const dir = key.startsWith("spec/") ? "spec" : "svc";
|
|
64
|
+
mkdirSync(join(root, "patches", dir), { recursive: true });
|
|
65
|
+
const name = key.replace(/^spec\//, "");
|
|
66
|
+
writeFileSync(join(root, "patches", dir, name), JSON.stringify({ patches: ops }));
|
|
67
|
+
}
|
|
68
|
+
mkdirSync(join(root, ".generated-specs"));
|
|
69
|
+
writeFileSync(join(root, ".generated-specs", "svc.json"), "committed\n");
|
|
70
|
+
return root;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const addField = (name: string) => ({ op: "add", path: `/fields/${name}`, value: true });
|
|
74
|
+
const addMember = (name: string) => ({
|
|
75
|
+
op: "add",
|
|
76
|
+
path: `/shapes/x#Thing/members/${name}`,
|
|
77
|
+
value: { target: "smithy.api#String" },
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("convert-stage patch audit", () => {
|
|
81
|
+
test("judges Smithy files in memory and the rest by convert", async () => {
|
|
82
|
+
const root = convertFixture({
|
|
83
|
+
"spec/1-spec.json": [addField("s")],
|
|
84
|
+
"2-member.json": [addMember("b")],
|
|
85
|
+
"3-noop.json": [{ op: "replace", path: "/shapes/x#Thing/type", value: "structure" }],
|
|
86
|
+
"4-dropped.json": [addMember("dropped")],
|
|
87
|
+
"5-uses-b.json": [{ op: "add", path: "/shapes/x#Thing/members/b/traits", value: {} }],
|
|
88
|
+
});
|
|
89
|
+
const result = await auditPackage(root, { jobs: 2 });
|
|
90
|
+
if (result.kind !== "audited") throw new Error(result.reason);
|
|
91
|
+
expect(Object.fromEntries(result.files.map((f) => [f.file.key, f.verdict]))).toEqual({
|
|
92
|
+
"spec/1-spec.json": { kind: "needed", diff: ["+ Thing/members/s"] },
|
|
93
|
+
"svc/2-member.json": { kind: "depended", error: expect.stringMatching(/members\/b/) },
|
|
94
|
+
"svc/3-noop.json": { kind: "unused" },
|
|
95
|
+
// The transform removes it again, so it changes nothing.
|
|
96
|
+
"svc/4-dropped.json": { kind: "unused" },
|
|
97
|
+
"svc/5-uses-b.json": { kind: "needed", diff: ["+ Thing/members/b/traits"] },
|
|
98
|
+
});
|
|
99
|
+
expect(result.judged).toEqual({ inMemory: 4, perFile: 1 });
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("--ops in memory finds the dead op", async () => {
|
|
103
|
+
const root = convertFixture({
|
|
104
|
+
"mixed.json": [
|
|
105
|
+
addMember("b"),
|
|
106
|
+
{ op: "replace", path: "/shapes/x#Thing/type", value: "structure" },
|
|
107
|
+
],
|
|
108
|
+
});
|
|
109
|
+
const result = await auditPackage(root, { ops: true, jobs: 1 });
|
|
110
|
+
if (result.kind !== "audited") throw new Error(result.reason);
|
|
111
|
+
expect(result.files[0]!.deadOps).toEqual([1]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("a file that only orders keys is needed", async () => {
|
|
115
|
+
const root = convertFixture({
|
|
116
|
+
"1-member.json": [addMember("b")],
|
|
117
|
+
"2-order.json": [
|
|
118
|
+
{
|
|
119
|
+
op: "move",
|
|
120
|
+
from: "/shapes/x#Thing/members/b",
|
|
121
|
+
path: "/shapes/x#Thing/members/b",
|
|
122
|
+
before: "a",
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
const result = await auditPackage(root, { jobs: 1 });
|
|
127
|
+
if (result.kind !== "audited") throw new Error(result.reason);
|
|
128
|
+
expect(result.files.find((f) => f.file.key === "svc/2-order.json")!.verdict).toEqual({
|
|
129
|
+
kind: "needed",
|
|
130
|
+
diff: ["(key order only)"],
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("leaves the package as it was and removes its scratch copies", async () => {
|
|
135
|
+
const root = convertFixture({
|
|
136
|
+
"member.json": [addMember("b")],
|
|
137
|
+
"spec/s.json": [addField("s")],
|
|
138
|
+
});
|
|
139
|
+
const result = await auditPackage(root, { jobs: 2 });
|
|
140
|
+
expect(result.kind).toBe("audited");
|
|
141
|
+
expect(readFileSync(join(root, ".generated-specs", "svc.json"), "utf8")).toBe("committed\n");
|
|
142
|
+
expect(readdirSync(join(root, ".."))).toEqual(["pkg"]);
|
|
143
|
+
expect(existsSync(join(root, ".audit-request.json"))).toBe(false);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("a patch that changes nothing has no effect", async () => {
|
|
147
|
+
const root = convertFixture({
|
|
148
|
+
"noop.json": [{ op: "replace", path: "/shapes/x#Thing/type", value: "structure" }],
|
|
149
|
+
});
|
|
150
|
+
const result = await auditPackage(root, { jobs: 1 });
|
|
151
|
+
if (result.kind !== "audited") throw new Error(result.reason);
|
|
152
|
+
expect(result.files[0]!.verdict).toEqual({ kind: "unused" });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("a baseline that fails to convert is skipped", async () => {
|
|
156
|
+
const root = convertFixture({ "stale.json": [{ op: "remove", path: "/shapes/x#Gone" }] });
|
|
157
|
+
const result = await auditPackage(root, { jobs: 1 });
|
|
158
|
+
expect(result.kind).toBe("skipped");
|
|
159
|
+
});
|
|
160
|
+
});
|