@skmtc/core 0.35.1 → 0.35.2

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.
Files changed (47) hide show
  1. package/esm/context/StackTrail.js +3 -13
  2. package/esm/context/documentTrail.d.ts +17 -0
  3. package/esm/context/documentTrail.js +28 -0
  4. package/esm/parse/pathParameters.d.ts +57 -0
  5. package/esm/parse/pathParameters.js +138 -0
  6. package/esm/parse/v3-0/operation/toOperationsV3.js +7 -1
  7. package/esm/parse/v3-0/parameter/toParameterV3.d.ts +7 -0
  8. package/esm/parse/v3-0/parameter/toParameterV3.js +19 -11
  9. package/esm/parse/v3-0/pathItem/toPathItemV3.d.ts +7 -1
  10. package/esm/parse/v3-0/pathItem/toPathItemV3.js +5 -3
  11. package/esm/parse/v3-0/webhook/toWebhooksV3.js +7 -1
  12. package/esm/parse/v3-1/operation/toOperationsV3.js +7 -1
  13. package/esm/parse/v3-1/parameter/toParameterV3.d.ts +7 -0
  14. package/esm/parse/v3-1/parameter/toParameterV3.js +19 -11
  15. package/esm/parse/v3-1/pathItem/toPathItemV3.d.ts +7 -1
  16. package/esm/parse/v3-1/pathItem/toPathItemV3.js +5 -3
  17. package/esm/parse/v3-1/webhook/toWebhooksV3.js +7 -1
  18. package/esm/parse/v3-2/operation/toOperationsV3.js +11 -10
  19. package/esm/parse/v3-2/parameter/toParameterV3.d.ts +7 -0
  20. package/esm/parse/v3-2/parameter/toParameterV3.js +30 -24
  21. package/esm/parse/v3-2/pathItem/toPathItemV3.d.ts +6 -13
  22. package/esm/parse/v3-2/pathItem/toPathItemV3.js +5 -30
  23. package/esm/parse/v3-2/webhook/toWebhooksV3.js +11 -10
  24. package/package.json +1 -1
  25. package/script/context/StackTrail.js +3 -13
  26. package/script/context/documentTrail.d.ts +17 -0
  27. package/script/context/documentTrail.js +34 -0
  28. package/script/parse/pathParameters.d.ts +57 -0
  29. package/script/parse/pathParameters.js +144 -0
  30. package/script/parse/v3-0/operation/toOperationsV3.js +7 -1
  31. package/script/parse/v3-0/parameter/toParameterV3.d.ts +7 -0
  32. package/script/parse/v3-0/parameter/toParameterV3.js +22 -13
  33. package/script/parse/v3-0/pathItem/toPathItemV3.d.ts +7 -1
  34. package/script/parse/v3-0/pathItem/toPathItemV3.js +4 -2
  35. package/script/parse/v3-0/webhook/toWebhooksV3.js +7 -1
  36. package/script/parse/v3-1/operation/toOperationsV3.js +7 -1
  37. package/script/parse/v3-1/parameter/toParameterV3.d.ts +7 -0
  38. package/script/parse/v3-1/parameter/toParameterV3.js +22 -13
  39. package/script/parse/v3-1/pathItem/toPathItemV3.d.ts +7 -1
  40. package/script/parse/v3-1/pathItem/toPathItemV3.js +4 -2
  41. package/script/parse/v3-1/webhook/toWebhooksV3.js +7 -1
  42. package/script/parse/v3-2/operation/toOperationsV3.js +10 -9
  43. package/script/parse/v3-2/parameter/toParameterV3.d.ts +7 -0
  44. package/script/parse/v3-2/parameter/toParameterV3.js +33 -26
  45. package/script/parse/v3-2/pathItem/toPathItemV3.d.ts +6 -13
  46. package/script/parse/v3-2/pathItem/toPathItemV3.js +5 -31
  47. package/script/parse/v3-2/webhook/toWebhooksV3.js +10 -9
@@ -2,7 +2,8 @@ import { toRequestBodyV3 } from '../requestBody/toRequestBodiesV3.js';
2
2
  import { toResponsesV3 } from '../response/toResponseV3.js';
3
3
  import { toParameterListV3 } from '../parameter/toParameterV3.js';
4
4
  import { OasOperation } from '../../../oas/operation/Operation.js';
5
- import { toPathItemResultV3 } from '../pathItem/toPathItemV3.js';
5
+ import { toPathItemV3 } from '../pathItem/toPathItemV3.js';
6
+ import { isEmpty } from '../../../helpers/isEmpty.js';
6
7
  import { toPathItemMethod } from '../methods.js';
7
8
  import { dropAdditionalOperationsV3 } from '../pathItem/dropAdditionalOperationsV3.js';
8
9
  import { warnQuerystringConflictsV3 } from '../parameter/warnQuerystringConflictsV3.js';
@@ -10,6 +11,7 @@ import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecif
10
11
  import { toSecurityRequirementsV3 } from '../securityRequirement/toSecurityRequirement.js';
11
12
  import { toExternalDocs } from '../externalDocs/toExternalDocs.js';
12
13
  import { toOptionalServersV3 } from '../server/toServerV3.js';
14
+ import { isDroppedByPathParameter } from '../../pathParameters.js';
13
15
  export const toOperationV3 = ({ operation, operationInfo, stackTrail, context }) => {
14
16
  const { method, path, pathItem } = operationInfo;
15
17
  const { operationId, tags, summary, description, parameters, requestBody, responses, deprecated, security, externalDocs, servers,
@@ -76,15 +78,11 @@ export const toOperationsV3 = ({ paths, stackTrail, context }) => {
76
78
  rest: fixedFields,
77
79
  methodObject: {}
78
80
  });
79
- const pathItemResult = toPathItemResultV3({
80
- pathItem: cleaned.rest,
81
- stackTrail: pathStack,
82
- context
83
- });
84
- if (!pathItemResult.valid) {
85
- return [];
86
- }
87
- const pathItemObject = pathItemResult.pathItem;
81
+ const pathItemResult = !isEmpty(cleaned.rest)
82
+ ? toPathItemV3({ pathItem: cleaned.rest, stackTrail: pathStack, context })
83
+ : undefined;
84
+ const pathItemObject = pathItemResult?.pathItem;
85
+ const brokenParameters = pathItemResult?.brokenParameters ?? [];
88
86
  return Object.entries(cleaned.methodObject)
89
87
  .map(([method, operation]) => {
90
88
  // Use `pathStack` (the trail with `path` already pushed),
@@ -96,6 +94,9 @@ export const toOperationsV3 = ({ paths, stackTrail, context }) => {
96
94
  if (!operation) {
97
95
  return;
98
96
  }
97
+ if (isDroppedByPathParameter({ brokenParameters, operation, stackTrail: st, context })) {
98
+ return undefined;
99
+ }
99
100
  try {
100
101
  return toOperationV3({
101
102
  operation,
@@ -4,12 +4,19 @@ import type { OpenAPIV3 } from 'openapi-types';
4
4
  import { OasParameter } from '../../../oas/parameter/Parameter.js';
5
5
  import type { OasRef } from '../../../oas/ref/Ref.js';
6
6
  import type { StackTrail } from '../../../context/StackTrail.js';
7
+ import { type PathParameterList } from '../../pathParameters.js';
7
8
  export type ToParameterListV3Args = {
8
9
  parameters: (OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject)[] | undefined;
9
10
  stackTrail: StackTrail;
10
11
  context: ParseContextType;
11
12
  };
12
13
  export declare const toParameterListV3: ({ parameters, stackTrail, context }: ToParameterListV3Args) => (OasParameter | OasRef<"parameter">)[] | undefined;
14
+ /**
15
+ * Path-level `parameters`: like {@link toParameterListV3}, but an entry
16
+ * that fails to parse is logged and returned in `broken` instead of
17
+ * throwing.
18
+ */
19
+ export declare const toPathParameterListV3: ({ parameters, stackTrail, context }: ToParameterListV3Args) => PathParameterList<OasParameter | OasRef<"parameter">>;
13
20
  export type ToParametersV3Args = {
14
21
  parameters: Record<string, OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject>;
15
22
  stackTrail: StackTrail;
@@ -9,31 +9,37 @@ import { OasParameter } from '../../../oas/parameter/Parameter.js';
9
9
  import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
10
10
  import * as v from 'valibot';
11
11
  import { isEmptyParameter, logEmptyParameter } from '../../emptyParameter.js';
12
- export const toParameterListV3 = ({ parameters, stackTrail, context }) => {
13
- if (!parameters) {
14
- return undefined;
12
+ import { toParameterEntries, toPathParameterList } from '../../pathParameters.js';
13
+ export const toParameterListV3 = ({ parameters, stackTrail, context }) => toParameterEntries({ parameters, stackTrail, context })?.flatMap((parameter, index) => toParameterEntryV3({ parameter, index, stackTrail, context }));
14
+ /**
15
+ * Path-level `parameters`: like {@link toParameterListV3}, but an entry
16
+ * that fails to parse is logged and returned in `broken` instead of
17
+ * throwing.
18
+ */
19
+ export const toPathParameterListV3 = ({ parameters, stackTrail, context }) => toPathParameterList({
20
+ parameters,
21
+ stackTrail,
22
+ context,
23
+ toEntry: ({ parameter, index }) => toParameterEntryV3({ parameter, index, stackTrail, context })
24
+ });
25
+ const toParameterEntryV3 = ({ parameter, index, stackTrail, context }) => {
26
+ if (isEmptyParameter(parameter)) {
27
+ stackTrail.trace(`${index}`, st => logEmptyParameter({ parameter, stackTrail: st, context }));
28
+ return [];
15
29
  }
16
- return parameters.flatMap((parameter, index) => {
17
- if (isEmptyParameter(parameter)) {
18
- stackTrail.trace(`${index}`, st => logEmptyParameter({ parameter, stackTrail: st, context }));
19
- return [];
20
- }
21
- const querystringIssue = toQuerystringIssue(parameter);
22
- if (querystringIssue) {
23
- context.logIssue({
24
- key: `${index}`,
25
- parent: parameter,
26
- level: 'error',
27
- message: querystringIssue,
28
- stackTrail,
29
- type: 'INVALID_PARAMETER'
30
- });
31
- return [];
32
- }
33
- return [
34
- stackTrail.trace(`${index}`, st => toParameterV3({ parameter, stackTrail: st, context }))
35
- ];
36
- });
30
+ const querystringIssue = toQuerystringIssue(parameter);
31
+ if (querystringIssue) {
32
+ context.logIssue({
33
+ key: `${index}`,
34
+ parent: parameter,
35
+ level: 'error',
36
+ message: querystringIssue,
37
+ stackTrail,
38
+ type: 'INVALID_PARAMETER'
39
+ });
40
+ return [];
41
+ }
42
+ return [stackTrail.trace(`${index}`, st => toParameterV3({ parameter, stackTrail: st, context }))];
37
43
  };
38
44
  /**
39
45
  * A `querystring` parameter describes the whole query string, so it must
@@ -1,5 +1,6 @@
1
1
  import type { OpenAPIV3 } from 'openapi-types';
2
2
  import type { ParseContextType } from '../../../context/parseTypes.js';
3
+ import type { BrokenPathParameter } from '../../pathParameters.js';
3
4
  import { OasPathItem } from '../../../oas/pathItem/PathItem.js';
4
5
  import type { StackTrail } from '../../../context/StackTrail.js';
5
6
  export type ToPathItemV3Args = {
@@ -7,17 +8,9 @@ export type ToPathItemV3Args = {
7
8
  stackTrail: StackTrail;
8
9
  context: ParseContextType;
9
10
  };
10
- export declare const toPathItemV3: ({ pathItem, stackTrail, context }: ToPathItemV3Args) => OasPathItem;
11
- export type PathItemResult = {
12
- valid: true;
13
- pathItem: OasPathItem | undefined;
14
- } | {
15
- valid: false;
11
+ export type PathItemV3 = {
12
+ pathItem: OasPathItem;
13
+ /** Path-level parameters that failed to parse; the caller drops each operation they apply to. */
14
+ brokenParameters: BrokenPathParameter[];
16
15
  };
17
- /**
18
- * Parses a path item's shared fields without letting a throw escape. A
19
- * path-level parameter that fails validation (for example `in: body`) logs
20
- * one `INVALID_OPERATION` at the path and returns `{ valid: false }`, so
21
- * the caller skips that path and keeps the rest.
22
- */
23
- export declare const toPathItemResultV3: ({ pathItem, stackTrail, context }: ToPathItemV3Args) => PathItemResult;
16
+ export declare const toPathItemV3: ({ pathItem, stackTrail, context }: ToPathItemV3Args) => PathItemV3;
@@ -1,15 +1,15 @@
1
- import { toParameterListV3 } from '../parameter/toParameterV3.js';
1
+ import { toPathParameterListV3 } from '../parameter/toParameterV3.js';
2
2
  import { OasPathItem } from '../../../oas/pathItem/PathItem.js';
3
3
  import { toOptionalServersV3 } from '../server/toServerV3.js';
4
4
  import { tryParseAt } from '../../../context/tryParseAt.js';
5
5
  import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
6
- import { isEmpty } from '../../../helpers/isEmpty.js';
7
6
  export const toPathItemV3 = ({ pathItem, stackTrail, context }) => {
8
7
  const { summary, description, parameters, servers, ...skipped } = pathItem;
9
- return new OasPathItem({
8
+ const pathParameters = stackTrail.trace('parameters', st => toPathParameterListV3({ parameters, stackTrail: st, context }));
9
+ const oasPathItem = new OasPathItem({
10
10
  summary,
11
11
  description,
12
- parameters: stackTrail.trace('parameters', st => toParameterListV3({ parameters, stackTrail: st, context })),
12
+ parameters: pathParameters.parameters,
13
13
  servers: tryParseAt({
14
14
  stackTrail,
15
15
  key: 'servers',
@@ -26,30 +26,5 @@ export const toPathItemV3 = ({ pathItem, stackTrail, context }) => {
26
26
  parentType: 'pathItem'
27
27
  })
28
28
  });
29
- };
30
- /**
31
- * Parses a path item's shared fields without letting a throw escape. A
32
- * path-level parameter that fails validation (for example `in: body`) logs
33
- * one `INVALID_OPERATION` at the path and returns `{ valid: false }`, so
34
- * the caller skips that path and keeps the rest.
35
- */
36
- export const toPathItemResultV3 = ({ pathItem, stackTrail, context }) => {
37
- if (isEmpty(pathItem)) {
38
- return { valid: true, pathItem: undefined };
39
- }
40
- try {
41
- return { valid: true, pathItem: toPathItemV3({ pathItem, stackTrail, context }) };
42
- }
43
- catch (error) {
44
- const normalized = error instanceof Error ? error : new Error(String(error));
45
- context.logIssueNoKey({
46
- parent: pathItem,
47
- level: 'error',
48
- message: normalized.message,
49
- cause: normalized,
50
- stackTrail,
51
- type: 'INVALID_OPERATION'
52
- });
53
- return { valid: false };
54
- }
29
+ return { pathItem: oasPathItem, brokenParameters: pathParameters.broken };
55
30
  };
@@ -2,7 +2,8 @@ import { toRequestBodyV3 } from '../requestBody/toRequestBodiesV3.js';
2
2
  import { toResponsesV3 } from '../response/toResponseV3.js';
3
3
  import { toParameterListV3 } from '../parameter/toParameterV3.js';
4
4
  import { OasWebhook } from '../../../oas/webhook/Webhook.js';
5
- import { toPathItemResultV3 } from '../pathItem/toPathItemV3.js';
5
+ import { toPathItemV3 } from '../pathItem/toPathItemV3.js';
6
+ import { isEmpty } from '../../../helpers/isEmpty.js';
6
7
  import { toPathItemMethod } from '../methods.js';
7
8
  import { dropAdditionalOperationsV3 } from '../pathItem/dropAdditionalOperationsV3.js';
8
9
  import { warnQuerystringConflictsV3 } from '../parameter/warnQuerystringConflictsV3.js';
@@ -10,6 +11,7 @@ import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecif
10
11
  import { toSecurityRequirementsV3 } from '../securityRequirement/toSecurityRequirement.js';
11
12
  import { toExternalDocs } from '../externalDocs/toExternalDocs.js';
12
13
  import { toOptionalServersV3 } from '../server/toServerV3.js';
14
+ import { isDroppedByPathParameter } from '../../pathParameters.js';
13
15
  /**
14
16
  * Parses a single webhook Operation Object into an {@link OasWebhook}.
15
17
  *
@@ -88,15 +90,11 @@ export const toWebhooksV3 = ({ webhooks, stackTrail, context }) => {
88
90
  rest: fixedFields,
89
91
  methodObject: {}
90
92
  });
91
- const pathItemResult = toPathItemResultV3({
92
- pathItem: cleaned.rest,
93
- stackTrail: nameStack,
94
- context
95
- });
96
- if (!pathItemResult.valid) {
97
- return [];
98
- }
99
- const pathItemObject = pathItemResult.pathItem;
93
+ const pathItemResult = !isEmpty(cleaned.rest)
94
+ ? toPathItemV3({ pathItem: cleaned.rest, stackTrail: nameStack, context })
95
+ : undefined;
96
+ const pathItemObject = pathItemResult?.pathItem;
97
+ const brokenParameters = pathItemResult?.brokenParameters ?? [];
100
98
  return Object.entries(cleaned.methodObject)
101
99
  .map(([method, operation]) => {
102
100
  // Use `nameStack` (the trail with the webhook name already
@@ -106,6 +104,9 @@ export const toWebhooksV3 = ({ webhooks, stackTrail, context }) => {
106
104
  if (!operation) {
107
105
  return;
108
106
  }
107
+ if (isDroppedByPathParameter({ brokenParameters, operation, stackTrail: st, context })) {
108
+ return undefined;
109
+ }
109
110
  try {
110
111
  return toWebhookV3({
111
112
  operation,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.35.1",
3
+ "version": "0.35.2",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -16,6 +16,7 @@ exports.StackTrail = void 0;
16
16
  require("../_dnt.polyfills.js");
17
17
  const Components_js_1 = require("../oas/components/Components.js");
18
18
  const JsonPointer_js_1 = require("../types/JsonPointer.js");
19
+ const documentTrail_js_1 = require("./documentTrail.js");
19
20
  class StackTrail {
20
21
  constructor(stack = []) {
21
22
  /** Internal stack of traversal frames */
@@ -214,15 +215,7 @@ class StackTrail {
214
215
  * ```
215
216
  */
216
217
  toSchemaPointer() {
217
- // Phase frames seeded by `CoreContext.toArtifacts` (PhaseType plus the
218
- // `post-pass` trace). The operational phase always sits before any
219
- // document key, so the FIRST match is the boundary — a document key
220
- // that happens to share a phase name (e.g. a schema literally named
221
- // `parse`) appears later and is unaffected.
222
- const phaseFrames = new Set(['parse', 'generate', 'render', 'post-pass']);
223
- const phaseIndex = __classPrivateFieldGet(this, _StackTrail_stack, "f").findIndex(frame => phaseFrames.has(frame));
224
- const documentFrames = phaseIndex === -1 ? __classPrivateFieldGet(this, _StackTrail_stack, "f") : __classPrivateFieldGet(this, _StackTrail_stack, "f").slice(phaseIndex + 1);
225
- return (0, JsonPointer_js_1.toJsonPointer)(documentFrames);
218
+ return (0, JsonPointer_js_1.toJsonPointer)((0, documentTrail_js_1.toDocumentFrames)(__classPrivateFieldGet(this, _StackTrail_stack, "f")));
226
219
  }
227
220
  toStackRef() {
228
221
  const [first, second, third] = this.stackTrail;
@@ -308,9 +301,7 @@ class StackTrail {
308
301
  * ```
309
302
  */
310
303
  toString() {
311
- return __classPrivateFieldGet(this, _StackTrail_stack, "f")
312
- .map(frame => frame.replace(/[%:]/g, character => escapes[character]))
313
- .join(':');
304
+ return (0, documentTrail_js_1.toTrailString)(__classPrivateFieldGet(this, _StackTrail_stack, "f"));
314
305
  }
315
306
  /**
316
307
  * Reads a string written by {@link StackTrail.toString} back into a trail.
@@ -334,5 +325,4 @@ class StackTrail {
334
325
  }
335
326
  exports.StackTrail = StackTrail;
336
327
  _StackTrail_stack = new WeakMap();
337
- const escapes = { '%': '%25', ':': '%3A' };
338
328
  const decodes = { '25': '%', '3A': ':' };
@@ -0,0 +1,17 @@
1
+ /** Joins trail frames with `:`, escaping `%` as `%25` and `:` as `%3A` inside a frame. */
2
+ export declare const toTrailString: (frames: string[]) => string;
3
+ /**
4
+ * The frames of a trail that address the input document: everything after
5
+ * the first phase frame, or every frame when there is no phase frame.
6
+ */
7
+ export declare const toDocumentFrames: (frames: string[]) => string[];
8
+ /**
9
+ * The document-relative trail in `StackTrail.toString` form.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * toDocumentString(['trace-1', 'span-1', 'parse', 'paths', '/pets', 'parameters', '0'])
14
+ * // 'paths:/pets:parameters:0'
15
+ * ```
16
+ */
17
+ export declare const toDocumentString: (frames: string[]) => string;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toDocumentString = exports.toDocumentFrames = exports.toTrailString = void 0;
4
+ const escapes = { '%': '%25', ':': '%3A' };
5
+ // Phase frames seeded by `CoreContext.toArtifacts` (PhaseType plus the
6
+ // `post-pass` trace).
7
+ const phaseFrames = new Set(['parse', 'generate', 'render', 'post-pass']);
8
+ /** Joins trail frames with `:`, escaping `%` as `%25` and `:` as `%3A` inside a frame. */
9
+ const toTrailString = (frames) => frames.map(frame => frame.replace(/[%:]/g, character => escapes[character])).join(':');
10
+ exports.toTrailString = toTrailString;
11
+ /**
12
+ * The frames of a trail that address the input document: everything after
13
+ * the first phase frame, or every frame when there is no phase frame.
14
+ */
15
+ const toDocumentFrames = (frames) => {
16
+ // The operational phase always sits before any document key, so the
17
+ // FIRST match is the boundary — a document key that happens to share a
18
+ // phase name (e.g. a schema literally named `parse`) appears later and
19
+ // is unaffected.
20
+ const phaseIndex = frames.findIndex(frame => phaseFrames.has(frame));
21
+ return phaseIndex === -1 ? frames : frames.slice(phaseIndex + 1);
22
+ };
23
+ exports.toDocumentFrames = toDocumentFrames;
24
+ /**
25
+ * The document-relative trail in `StackTrail.toString` form.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * toDocumentString(['trace-1', 'span-1', 'parse', 'paths', '/pets', 'parameters', '0'])
30
+ * // 'paths:/pets:parameters:0'
31
+ * ```
32
+ */
33
+ const toDocumentString = (frames) => (0, exports.toTrailString)((0, exports.toDocumentFrames)(frames));
34
+ exports.toDocumentString = toDocumentString;
@@ -0,0 +1,57 @@
1
+ import type { ParseContextType } from '../context/parseTypes.js';
2
+ import type { StackTrail } from '../context/StackTrail.js';
3
+ /**
4
+ * Reads a `parameters` field. `undefined` is no parameters. `{}` or `null`
5
+ * is read as no parameters with an `EMPTY_PARAMETER` warning at the
6
+ * field. Any other value that is not an array throws.
7
+ */
8
+ export declare const toParameterEntries: <Entry>({ parameters, stackTrail, context }: {
9
+ parameters: Entry[] | undefined;
10
+ /** Trail that ends in `parameters`. */
11
+ stackTrail: StackTrail;
12
+ context: ParseContextType;
13
+ }) => Entry[] | undefined;
14
+ /** A path-level parameter entry, or the whole field, that failed to parse. */
15
+ export type BrokenPathParameter = {
16
+ /** Document-relative trail of the entry, or of the `parameters` field when it is not an array. */
17
+ location: string;
18
+ /** Set only when the raw entry has string `name` and `in`. */
19
+ name?: string;
20
+ in?: string;
21
+ message: string;
22
+ };
23
+ export type PathParameterList<Parameter> = {
24
+ parameters: Parameter[] | undefined;
25
+ broken: BrokenPathParameter[];
26
+ };
27
+ export type ToPathParameterListArgs<Entry, Parameter> = {
28
+ parameters: Entry[] | undefined;
29
+ /** Trail that ends in `parameters`. */
30
+ stackTrail: StackTrail;
31
+ context: ParseContextType;
32
+ /** The tree's parser for one list entry, as used by `toParameterListV3`. */
33
+ toEntry: (args: {
34
+ parameter: Entry;
35
+ index: number;
36
+ }) => Parameter[];
37
+ };
38
+ /**
39
+ * Parses a path item's `parameters` without letting a throw escape. Each
40
+ * entry that fails logs one `INVALID_PARAMETER` error at its index and is
41
+ * returned in `broken`; a field that is not an array logs one at the field.
42
+ * The caller decides per operation which broken entries apply.
43
+ */
44
+ export declare const toPathParameterList: <Entry, Parameter>({ parameters, stackTrail, context, toEntry }: ToPathParameterListArgs<Entry, Parameter>) => PathParameterList<Parameter>;
45
+ /**
46
+ * True when a broken path-level parameter applies to the operation. Logs
47
+ * one `INVALID_OPERATION` error at the operation that names the first
48
+ * such entry.
49
+ */
50
+ export declare const isDroppedByPathParameter: ({ brokenParameters, operation, stackTrail, context }: {
51
+ brokenParameters: BrokenPathParameter[];
52
+ /** The raw operation object. */
53
+ operation: unknown;
54
+ /** Trail that ends in the operation's method. */
55
+ stackTrail: StackTrail;
56
+ context: ParseContextType;
57
+ }) => boolean;
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isDroppedByPathParameter = exports.toPathParameterList = exports.toParameterEntries = void 0;
4
+ const emptyParameter_js_1 = require("./emptyParameter.js");
5
+ const documentTrail_js_1 = require("../context/documentTrail.js");
6
+ /**
7
+ * Reads a `parameters` field. `undefined` is no parameters. `{}` or `null`
8
+ * is read as no parameters with an `EMPTY_PARAMETER` warning at the
9
+ * field. Any other value that is not an array throws.
10
+ */
11
+ const toParameterEntries = ({ parameters, stackTrail, context }) => {
12
+ if (parameters === undefined) {
13
+ return undefined;
14
+ }
15
+ if (Array.isArray(parameters)) {
16
+ return parameters;
17
+ }
18
+ if ((0, emptyParameter_js_1.isEmptyParameter)(parameters)) {
19
+ context.logIssueNoKey({
20
+ stackTrail,
21
+ parent: parameters,
22
+ level: 'warning',
23
+ type: 'EMPTY_PARAMETER',
24
+ message: 'Empty parameters field; read as no parameters'
25
+ });
26
+ return undefined;
27
+ }
28
+ throw new Error(`Expected parameters to be an array but received ${typeof parameters}`);
29
+ };
30
+ exports.toParameterEntries = toParameterEntries;
31
+ const toError = (error) => error instanceof Error ? error : new Error(String(error));
32
+ const toNameAndIn = (parameter) => {
33
+ if (typeof parameter === 'object' &&
34
+ parameter !== null &&
35
+ 'name' in parameter &&
36
+ 'in' in parameter &&
37
+ typeof parameter.name === 'string' &&
38
+ typeof parameter.in === 'string') {
39
+ return { name: parameter.name, in: parameter.in };
40
+ }
41
+ return {};
42
+ };
43
+ /**
44
+ * Parses a path item's `parameters` without letting a throw escape. Each
45
+ * entry that fails logs one `INVALID_PARAMETER` error at its index and is
46
+ * returned in `broken`; a field that is not an array logs one at the field.
47
+ * The caller decides per operation which broken entries apply.
48
+ */
49
+ const toPathParameterList = ({ parameters, stackTrail, context, toEntry }) => {
50
+ const logBroken = (st, parent, error) => {
51
+ const normalized = toError(error);
52
+ context.logIssueNoKey({
53
+ level: 'error',
54
+ stackTrail: st,
55
+ parent,
56
+ message: normalized.message,
57
+ cause: normalized,
58
+ type: 'INVALID_PARAMETER'
59
+ });
60
+ return { location: (0, documentTrail_js_1.toDocumentString)(st.stackTrail), message: normalized.message };
61
+ };
62
+ const readEntries = () => {
63
+ try {
64
+ return { entries: (0, exports.toParameterEntries)({ parameters, stackTrail, context }) };
65
+ }
66
+ catch (error) {
67
+ // A field that is not an array cannot be matched to an override.
68
+ return { broken: logBroken(stackTrail, parameters, error) };
69
+ }
70
+ };
71
+ const read = readEntries();
72
+ if ('broken' in read) {
73
+ return { parameters: undefined, broken: [read.broken] };
74
+ }
75
+ if (read.entries === undefined) {
76
+ return { parameters: undefined, broken: [] };
77
+ }
78
+ const results = read.entries.map((parameter, index) => {
79
+ try {
80
+ return { parsed: toEntry({ parameter, index }), broken: [] };
81
+ }
82
+ catch (error) {
83
+ return {
84
+ parsed: [],
85
+ broken: [
86
+ {
87
+ ...stackTrail.trace(`${index}`, st => logBroken(st, parameter, error)),
88
+ ...toNameAndIn(parameter)
89
+ }
90
+ ]
91
+ };
92
+ }
93
+ });
94
+ return {
95
+ parameters: results.flatMap(({ parsed }) => parsed),
96
+ broken: results.flatMap(({ broken }) => broken)
97
+ };
98
+ };
99
+ exports.toPathParameterList = toPathParameterList;
100
+ /**
101
+ * The first broken path-level parameter that applies to an operation. An
102
+ * entry applies unless it has a readable `name` and `in` and the
103
+ * operation's inline `parameters` hold the same pair, which overrides it.
104
+ * A `$ref` operation parameter cannot be resolved during parse, so it
105
+ * overrides nothing.
106
+ */
107
+ const toApplyingBrokenParameter = ({ broken, operation }) => {
108
+ if (broken.length === 0) {
109
+ return undefined;
110
+ }
111
+ const operationParameters = typeof operation === 'object' &&
112
+ operation !== null &&
113
+ 'parameters' in operation &&
114
+ Array.isArray(operation.parameters)
115
+ ? operation.parameters
116
+ : [];
117
+ const overrides = new Set(operationParameters.flatMap((parameter) => {
118
+ const { name, in: parameterIn } = toNameAndIn(parameter);
119
+ return name === undefined || parameterIn === undefined ? [] : [`${parameterIn}:${name}`];
120
+ }));
121
+ return broken.find(entry => entry.name === undefined ||
122
+ entry.in === undefined ||
123
+ !overrides.has(`${entry.in}:${entry.name}`));
124
+ };
125
+ /**
126
+ * True when a broken path-level parameter applies to the operation. Logs
127
+ * one `INVALID_OPERATION` error at the operation that names the first
128
+ * such entry.
129
+ */
130
+ const isDroppedByPathParameter = ({ brokenParameters, operation, stackTrail, context }) => {
131
+ const broken = toApplyingBrokenParameter({ broken: brokenParameters, operation });
132
+ if (!broken) {
133
+ return false;
134
+ }
135
+ context.logIssueNoKey({
136
+ parent: operation,
137
+ level: 'error',
138
+ message: `Path-level parameter ${broken.location} failed to parse: ${broken.message}`,
139
+ stackTrail,
140
+ type: 'INVALID_OPERATION'
141
+ });
142
+ return true;
143
+ };
144
+ exports.isDroppedByPathParameter = isDroppedByPathParameter;
@@ -12,6 +12,7 @@ const toSecurityRequirement_js_1 = require("../securityRequirement/toSecurityReq
12
12
  const isEmpty_js_1 = require("../../../helpers/isEmpty.js");
13
13
  const toExternalDocs_js_1 = require("../externalDocs/toExternalDocs.js");
14
14
  const toServerV3_js_1 = require("../server/toServerV3.js");
15
+ const pathParameters_js_1 = require("../../pathParameters.js");
15
16
  const toOperationV3 = ({ operation, operationInfo, stackTrail, context }) => {
16
17
  const { method, path, pathItem } = operationInfo;
17
18
  const { operationId, tags, summary, description, parameters, requestBody, responses, deprecated, security, externalDocs, servers,
@@ -74,9 +75,11 @@ const toOperationsV3 = ({ paths, stackTrail, context }) => {
74
75
  rest: pathItem,
75
76
  methodObject: {}
76
77
  });
77
- const pathItemObject = !(0, isEmpty_js_1.isEmpty)(cleaned.rest)
78
+ const pathItemResult = !(0, isEmpty_js_1.isEmpty)(cleaned.rest)
78
79
  ? (0, toPathItemV3_js_1.toPathItemV3)({ pathItem: cleaned.rest, stackTrail: pathStack, context })
79
80
  : undefined;
81
+ const pathItemObject = pathItemResult?.pathItem;
82
+ const brokenParameters = pathItemResult?.brokenParameters ?? [];
80
83
  return Object.entries(cleaned.methodObject)
81
84
  .map(([method, operation]) => {
82
85
  // Use `pathStack` (the trail with `path` already pushed),
@@ -88,6 +91,9 @@ const toOperationsV3 = ({ paths, stackTrail, context }) => {
88
91
  if (!operation) {
89
92
  return;
90
93
  }
94
+ if ((0, pathParameters_js_1.isDroppedByPathParameter)({ brokenParameters, operation, stackTrail: st, context })) {
95
+ return undefined;
96
+ }
91
97
  try {
92
98
  return (0, exports.toOperationV3)({
93
99
  operation,
@@ -4,6 +4,7 @@ import type { OpenAPIV3 } from 'openapi-types';
4
4
  import { OasParameter } from '../../../oas/parameter/Parameter.js';
5
5
  import type { OasRef } from '../../../oas/ref/Ref.js';
6
6
  import type { StackTrail } from '../../../context/StackTrail.js';
7
+ import { type PathParameterList } from '../../pathParameters.js';
7
8
  type ParameterLocation = Exclude<OasParameterLocation, 'querystring'>;
8
9
  type ParameterStyle = Exclude<OasParameterStyle, 'cookie'>;
9
10
  export type ToParameterListV3Args = {
@@ -12,6 +13,12 @@ export type ToParameterListV3Args = {
12
13
  context: ParseContextType;
13
14
  };
14
15
  export declare const toParameterListV3: ({ parameters, stackTrail, context }: ToParameterListV3Args) => (OasParameter | OasRef<"parameter">)[] | undefined;
16
+ /**
17
+ * Path-level `parameters`: like {@link toParameterListV3}, but an entry
18
+ * that fails to parse is logged and returned in `broken` instead of
19
+ * throwing.
20
+ */
21
+ export declare const toPathParameterListV3: ({ parameters, stackTrail, context }: ToParameterListV3Args) => PathParameterList<OasParameter | OasRef<"parameter">>;
15
22
  export type ToParametersV3Args = {
16
23
  parameters: Record<string, OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject>;
17
24
  stackTrail: StackTrail;