Skip to content

Commit

Permalink
Fix inability to have any type under additionalProperties
Browse files Browse the repository at this point in the history
analyzeTypeUsage() incorrectly coerces an empty type to `object` - see
matrix-org/matrix-spec#1115 for the case that
breaks things (even with this PR merged the generated type remained
`string->object` instead of `string->`).
  • Loading branch information
KitsuneRal committed May 3, 2024
1 parent 80b1f0a commit 607938c
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ TypeUsage Analyzer::analyzeTypeUsage(const YamlMap<>& node)
if (yamlTypeNode && yamlTypeNode->IsSequence())
return analyzeMultitype(yamlTypeNode->as<YamlSequence<>>());

auto yamlType = yamlTypeNode ? yamlTypeNode->as<string>() : "object"s;
auto yamlType = yamlTypeNode ? yamlTypeNode->as<string>() : ""s;
if (yamlType == "array")
{
if (auto yamlElemType = node.maybeGet<YamlMap<>>("items"); !yamlElemType.empty()) {
Expand All @@ -93,7 +93,7 @@ TypeUsage Analyzer::analyzeTypeUsage(const YamlMap<>& node)

return _translator.mapType("array");
}
if (yamlType == "object")
if (yamlType.empty() || yamlType == "object")
{
auto schema = analyzeSchema(node);
if (schema.maxProperties == 0)
Expand All @@ -109,15 +109,10 @@ TypeUsage Analyzer::analyzeTypeUsage(const YamlMap<>& node)
if (!schema.name.empty()) // Only ever filled for non-empty schemas
return addSchema(std::move(schema)); // Wrap `schema` in a TypeUsage

// An empty object is schemaless but existing, mapType("object")
// Also, a nameless non-empty schema is treated as a generic
// mapType("object") for now. TODO, low priority: ad-hoc typing (via tuples?)
// An empty type is schemaless but existing, while a nameless non-empty schema is treated
// as a generic mapType("object") for now. TODO, low priority: ad-hoc typing (via tuples?)
}
if (const auto tu = _translator.mapType(yamlType, node.get<string_view>("format", {}));
!tu.empty())
return tu;

throw YamlException(node, "Unknown type: " + yamlType);
return _translator.mapType(yamlType, node.get<string_view>("format", {}));
}

TypeUsage Analyzer::addSchema(ObjectSchema&& schema)
Expand Down

0 comments on commit 607938c

Please sign in to comment.