Skip to content

Commit f2462c1

Browse files
committed
fix: type error on additionalItems
Fixes the type error caused by the type definition of `JsonSchema.additionalItems` which was fixed in `aws-cdk-lib` v2.201.0. Since `JsonSchema.additionalItems` and `JsonSchema.additionalProperties` have the same type signature, introduces a new translation function `translateBooleanOrSchemaProperty` for them. They are now grouped as `BOOLEAN_OR_SCHEMA_PROPERTIES`.
1 parent bf19eee commit f2462c1

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/json-schema-ex.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface JsonSchemaEx extends apigateway.JsonSchema {
2828
* Extension of
2929
* {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.JsonSchema.html#additionalitems | additionalItems}.
3030
*/
31-
additionalItems?: JsonSchemaEx[];
31+
additionalItems?: boolean | JsonSchemaEx;
3232
/**
3333
* Extension of
3434
* {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.JsonSchema.html#additionalproperties | additionalProperties}.
@@ -136,7 +136,6 @@ type SingleSchemaProperty = typeof SINGLE_SCHEMA_PROPERTIES[number];
136136
* → `JsonSchema[]`
137137
*/
138138
const ARRAY_SCHEMA_PROPERTIES = [
139-
'additionalItems',
140139
'allOf',
141140
'anyOf',
142141
'oneOf',
@@ -166,6 +165,17 @@ const MAP_SCHEMA_PROPERTIES = [
166165
] as const;
167166
type MapSchemaProperty = typeof MAP_SCHEMA_PROPERTIES[number];
168167

168+
/**
169+
* Boolean or schema properties of `JsonSchemaEx`.
170+
*
171+
* → `boolean | JsonSchema`
172+
*/
173+
const BOOLEAN_OR_SCHEMA_PROPERTIES = [
174+
'additionalItems',
175+
'additionalProperties',
176+
] as const;
177+
type BooleanOrSchemaProperty = typeof BOOLEAN_OR_SCHEMA_PROPERTIES[number];
178+
169179
/**
170180
* Output type of {@link translateJsonSchemaEx}.
171181
*
@@ -258,18 +268,10 @@ export function translateJsonSchemaEx(
258268
for (const prop of MAP_SCHEMA_PROPERTIES) {
259269
translateProperty(prop, translateMapSchemaProperty);
260270
}
271+
for (const prop of BOOLEAN_OR_SCHEMA_PROPERTIES) {
272+
translateProperty(prop, translateBooleanOrSchemaProperty);
273+
}
261274
// deals with corner cases
262-
// - additionalProperties: boolean | JsonSchemaEx
263-
translateProperty('additionalProperties', (_, value) => {
264-
if (typeof value === 'boolean') {
265-
return {
266-
gatewayValue: value,
267-
openapiValue: value,
268-
};
269-
} else {
270-
return translateSingleSchemaProperty(restApi, value);
271-
}
272-
});
273275
// - definitions: { [k: string]: JsonSchemaEx | string[] }
274276
translateProperty('dependencies', (_, map) => {
275277
const gatewayValue: { [k: string]: apigateway.JsonSchema | string[] } = {};
@@ -407,3 +409,21 @@ function translateMapSchemaProperty(
407409
openapiValue,
408410
};
409411
}
412+
413+
/** Translates a boolean or schema property. */
414+
function translateBooleanOrSchemaProperty(
415+
restApi: apigateway.IRestApi,
416+
value: boolean | JsonSchemaEx,
417+
): {
418+
gatewayValue: boolean | apigateway.JsonSchema,
419+
openapiValue: boolean | JsonSchemaEx,
420+
} {
421+
if (typeof value === 'boolean') {
422+
return {
423+
gatewayValue: value,
424+
openapiValue: value,
425+
};
426+
} else {
427+
return translateSingleSchemaProperty(restApi, value);
428+
}
429+
}

0 commit comments

Comments
 (0)