diff --git a/packages/quicktype-core/src/input/JSONSchemaInput.ts b/packages/quicktype-core/src/input/JSONSchemaInput.ts index 66b1c4104..ab1f16ce4 100644 --- a/packages/quicktype-core/src/input/JSONSchemaInput.ts +++ b/packages/quicktype-core/src/input/JSONSchemaInput.ts @@ -1139,14 +1139,20 @@ async function addTypesInSchema( } let additionalProperties = schema.additionalProperties; - // This is an incorrect hack to fix an issue with a Go->Schema generator: - // https://github.com/quicktype/quicktype/issues/976 + // Treat a single pattern property as additional properties since + // per-pattern typed properties aren't supported. if ( additionalProperties === undefined && typeof schema.patternProperties === "object" && - hasOwnProperty(schema.patternProperties, ".*") + schema.patternProperties !== null ) { - additionalProperties = schema.patternProperties[".*"]; + const patterns = Object.getOwnPropertyNames( + schema.patternProperties, + ); + if (patterns.length === 1) { + additionalProperties = + schema.patternProperties[patterns[0]]; + } } // Handle unevaluatedProperties if additionalProperties is not defined diff --git a/test/inputs/schema/pattern-properties.1.fail.json b/test/inputs/schema/pattern-properties.1.fail.json new file mode 100644 index 000000000..088c5fadd --- /dev/null +++ b/test/inputs/schema/pattern-properties.1.fail.json @@ -0,0 +1,8 @@ +{ + "alternators": { + "1": { + "voltage": "not a number", + "name": "Main alternator" + } + } +} diff --git a/test/inputs/schema/pattern-properties.1.json b/test/inputs/schema/pattern-properties.1.json new file mode 100644 index 000000000..fda2c9915 --- /dev/null +++ b/test/inputs/schema/pattern-properties.1.json @@ -0,0 +1,12 @@ +{ + "alternators": { + "1": { + "voltage": 14.2, + "name": "Main alternator" + }, + "backup": { + "voltage": 13.8, + "name": "Backup alternator" + } + } +} diff --git a/test/inputs/schema/pattern-properties.schema b/test/inputs/schema/pattern-properties.schema new file mode 100644 index 000000000..c22ba2338 --- /dev/null +++ b/test/inputs/schema/pattern-properties.schema @@ -0,0 +1,19 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Electrical", + "type": "object", + "properties": { + "alternators": { + "type": "object", + "patternProperties": { + "(^[A-Za-z0-9]+$)": { + "type": "object", + "properties": { + "voltage": { "type": "number" }, + "name": { "type": "string" } + } + } + } + } + } +} diff --git a/test/languages.ts b/test/languages.ts index 120f3170b..4fd5e3c82 100644 --- a/test/languages.ts +++ b/test/languages.ts @@ -55,6 +55,7 @@ const skipsUntypedUnions = [ // schema whose fail sample relies on rejecting a mistyped map value. const skipsMapValueValidation = [ "go-schema-pattern-properties.schema", + "pattern-properties.schema", "unevaluated-properties.schema", ];