Skip to content

Commit d850048

Browse files
authored
fix(const-to-enum): handle boolean schema in properties (#1845)
JSON Schema allows `true` as a shorthand for an empty schema that accepts any value. The `constToEnum` transformer was failing when encountering such boolean schemas in properties because the `hasConst` type guard assumed the input was always an object. This adds a type check to handle boolean schemas gracefully, preventing runtime errors during transformation. --- ### Ask Yourself - [x] Have you reviewed the [contribution guide](https://github.com/cdklabs/json2jsii/blob/main/CONTRIBUTING.md)? - [x] Have you reviewed the [breaking changes guide](https://github.com/cdklabs/json2jsii/blob/main/CONTRIBUTING.md#breaking-changes)?
1 parent 24b5759 commit d850048

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/transformers/const-to-enum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function transformConstToEnum(def: JSONSchema4): JSONSchema4 {
7474
* Type guard to check if the schema has a `const` property defined.
7575
*/
7676
function hasConst(def: JSONSchema4): boolean {
77-
return 'const' in def && def.const !== undefined;
77+
return typeof def === 'object' && 'const' in def && def.const !== undefined;
7878
}
7979

8080
/**

test/const-to-enum.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,19 @@ describe('constToEnum', () => {
309309
expect(result.oneOf?.[0].const).toBeUndefined();
310310
expect(result.oneOf?.[1].enum).toEqual(['b', 'c']);
311311
});
312+
313+
test('should handle required property with boolean true schema', () => {
314+
const schema: JSONSchema4 = {
315+
required: ['field'],
316+
properties: {
317+
field: true as any,
318+
},
319+
};
320+
321+
const result = constToEnum(schema);
322+
323+
expect(result.required).toEqual(['field']);
324+
expect(result.properties?.field).toBe(true);
325+
});
312326
});
313327
});

test/prohibited-names.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fc from 'fast-check';
12
import { JSONSchema4 } from 'json-schema';
23
import { TypeGenerator } from '../src';
34

@@ -106,8 +107,6 @@ describe('prohibited member names', () => {
106107
});
107108

108109

109-
import * as fc from 'fast-check';
110-
111110
/**
112111
* Property-based tests for prohibited name handling
113112
* Feature: reserved-word-property-names

0 commit comments

Comments
 (0)