Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/quicktype-core/src/input/JSONSchemaInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/inputs/schema/pattern-properties.1.fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"alternators": {
"1": {
"voltage": "not a number",
"name": "Main alternator"
}
}
}
12 changes: 12 additions & 0 deletions test/inputs/schema/pattern-properties.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"alternators": {
"1": {
"voltage": 14.2,
"name": "Main alternator"
},
"backup": {
"voltage": 13.8,
"name": "Backup alternator"
}
}
}
19 changes: 19 additions & 0 deletions test/inputs/schema/pattern-properties.schema
Original file line number Diff line number Diff line change
@@ -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" }
}
}
}
}
}
}
1 change: 1 addition & 0 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
];

Expand Down
Loading