idl_gen_json_schema.cpp: Changed generation of array element types (#6253)

* idl_gen_json_schema.cpp: Changed generation of array element types
#6175

* idl_gen_json_schema.cpp: Simplified indent generation as suggested by @vglavnyy
#6175
This commit is contained in:
schoetbi
2020-11-20 01:17:03 +01:00
committed by GitHub
parent 25eba6f35f
commit 69a8b2a579
3 changed files with 39 additions and 30 deletions

View File

@@ -87,17 +87,27 @@ std::string GenType(BaseType type) {
std::string GenBaseType(const Type &type) { std::string GenBaseType(const Type &type) {
if (type.struct_def != nullptr) { return GenTypeRef(type.struct_def); } if (type.struct_def != nullptr) { return GenTypeRef(type.struct_def); }
if (type.enum_def != nullptr) { return GenTypeRef(type.enum_def); } if (type.enum_def != nullptr) { return GenTypeRef(type.enum_def); }
if (IsArray(type) || IsVector(type)) { return GenType(type.base_type);
return "\"type\" : \"array\", \"items\" : {" + GenType(type.element) + "}"; }
std::string GenArrayType(const Type &type) {
std::string element_type;
if (type.struct_def != nullptr) {
element_type = GenTypeRef(type.struct_def);
} else if (type.enum_def != nullptr) {
element_type = GenTypeRef(type.enum_def);
} else {
element_type = GenType(type.element);
} }
return GenType(type.base_type);
return "\"type\" : \"array\", \"items\" : {" + element_type + "}";
} }
std::string GenType(const Type &type) { std::string GenType(const Type &type) {
switch (type.base_type) { switch (type.base_type) {
case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru
case BASE_TYPE_VECTOR: { case BASE_TYPE_VECTOR: {
return GenBaseType(type); return GenArrayType(type);
} }
case BASE_TYPE_STRUCT: { case BASE_TYPE_STRUCT: {
return GenTypeRef(type.struct_def); return GenTypeRef(type.struct_def);
@@ -147,13 +157,13 @@ class JsonSchemaGenerator : public BaseGenerator {
// If indentation is less than 0, that indicates we don't want any newlines // If indentation is less than 0, that indicates we don't want any newlines
// either. // either.
const std::string NewLine() { std::string NewLine() const {
return parser_.opts.indent_step >= 0 ? "\n" : ""; return parser_.opts.indent_step >= 0 ? "\n" : "";
} }
const std::string Indent(int indent) { std::string Indent(int indent) const {
std::string indentation = ""; const auto num_spaces = indent * std::max(parser_.opts.indent_step, 0);
return indentation.append(indent * std::max(parser_.opts.indent_step, 0), ' '); return std::string(num_spaces, ' ');
} }
bool generate() { bool generate() {
@@ -168,7 +178,7 @@ class JsonSchemaGenerator : public BaseGenerator {
++e) { ++e) {
code_ += Indent(2) + "\"" + GenFullName(*e) + "\" : {" + NewLine(); code_ += Indent(2) + "\"" + GenFullName(*e) + "\" : {" + NewLine();
code_ += Indent(3) + GenType("string") + "," + NewLine(); code_ += Indent(3) + GenType("string") + "," + NewLine();
std::string enumdef(Indent(3) + "\"enum\": ["); auto enumdef(Indent(3) + "\"enum\": [");
for (auto enum_value = (*e)->Vals().begin(); for (auto enum_value = (*e)->Vals().begin();
enum_value != (*e)->Vals().end(); ++enum_value) { enum_value != (*e)->Vals().end(); ++enum_value) {
enumdef.append("\"" + (*enum_value)->name + "\""); enumdef.append("\"" + (*enum_value)->name + "\"");
@@ -189,7 +199,7 @@ class JsonSchemaGenerator : public BaseGenerator {
comment_line != comment_lines.cend(); ++comment_line) { comment_line != comment_lines.cend(); ++comment_line) {
comment.append(*comment_line); comment.append(*comment_line);
} }
if (comment.size() > 0) { if (!comment.empty()) {
std::string description; std::string description;
if (!EscapeString(comment.c_str(), comment.length(), &description, true, if (!EscapeString(comment.c_str(), comment.length(), &description, true,
true)) { true)) {
@@ -206,13 +216,14 @@ class JsonSchemaGenerator : public BaseGenerator {
std::string arrayInfo = ""; std::string arrayInfo = "";
if (IsArray(property->value.type)) { if (IsArray(property->value.type)) {
arrayInfo = "," + NewLine() + Indent(8) + "\"minItems\": " + arrayInfo = "," + NewLine() + Indent(8) + "\"minItems\": " +
NumToString(property->value.type.fixed_length) + NumToString(property->value.type.fixed_length) + "," +
"," + NewLine() + Indent(8) + "\"maxItems\": " + NewLine() + Indent(8) + "\"maxItems\": " +
NumToString(property->value.type.fixed_length); NumToString(property->value.type.fixed_length);
} }
std::string deprecated_info = ""; std::string deprecated_info = "";
if (property->deprecated) { if (property->deprecated) {
deprecated_info = "," + NewLine() + Indent(8) + "\"deprecated\" : true,"; deprecated_info =
"," + NewLine() + Indent(8) + "\"deprecated\" : true,";
} }
std::string typeLine = Indent(4) + "\"" + property->name + "\""; std::string typeLine = Indent(4) + "\"" + property->name + "\"";
typeLine += " : {" + NewLine() + Indent(8); typeLine += " : {" + NewLine() + Indent(8);
@@ -229,8 +240,8 @@ class JsonSchemaGenerator : public BaseGenerator {
std::copy_if(properties.begin(), properties.end(), std::copy_if(properties.begin(), properties.end(),
back_inserter(requiredProperties), back_inserter(requiredProperties),
[](FieldDef const *prop) { return prop->required; }); [](FieldDef const *prop) { return prop->required; });
if (requiredProperties.size() > 0) { if (!requiredProperties.empty()) {
std::string required_string(Indent(3) + "\"required\" : ["); auto required_string(Indent(3) + "\"required\" : [");
for (auto req_prop = requiredProperties.cbegin(); for (auto req_prop = requiredProperties.cbegin();
req_prop != requiredProperties.cend(); ++req_prop) { req_prop != requiredProperties.cend(); ++req_prop) {
required_string.append("\"" + (*req_prop)->name + "\""); required_string.append("\"" + (*req_prop)->name + "\"");
@@ -242,7 +253,7 @@ class JsonSchemaGenerator : public BaseGenerator {
code_ += required_string + NewLine(); code_ += required_string + NewLine();
} }
code_ += Indent(3) + "\"additionalProperties\" : false" + NewLine(); code_ += Indent(3) + "\"additionalProperties\" : false" + NewLine();
std::string closeType(Indent(2) + "}"); auto closeType(Indent(2) + "}");
if (*s != parser_.structs_.vec.back()) { closeType.append(","); } if (*s != parser_.structs_.vec.back()) { closeType.append(","); }
code_ += closeType + NewLine(); // close type code_ += closeType + NewLine(); // close type
} }
@@ -256,15 +267,13 @@ class JsonSchemaGenerator : public BaseGenerator {
return true; return true;
} }
bool save() { bool save() const {
const std::string file_path = const auto file_path =
GeneratedFileName(path_, file_name_, parser_.opts); GeneratedFileName(path_, file_name_, parser_.opts);
return SaveFile(file_path.c_str(), code_, false); return SaveFile(file_path.c_str(), code_, false);
} }
const std::string getJson() { const std::string getJson() { return code_; }
return code_;
}
}; };
} // namespace jsons } // namespace jsons

View File

@@ -17,7 +17,7 @@
"$ref" : "#/definitions/MyGame_Example_TestEnum" "$ref" : "#/definitions/MyGame_Example_TestEnum"
}, },
"c" : { "c" : {
"$ref" : "#/definitions/MyGame_Example_TestEnum", "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_TestEnum"},
"minItems": 2, "minItems": 2,
"maxItems": 2 "maxItems": 2
}, },
@@ -44,7 +44,7 @@
"type" : "integer", "minimum" : -128, "maximum" : 127" "type" : "integer", "minimum" : -128, "maximum" : 127"
}, },
"d" : { "d" : {
"$ref" : "#/definitions/MyGame_Example_NestedStruct", "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_NestedStruct"},
"minItems": 2, "minItems": 2,
"maxItems": 2 "maxItems": 2
}, },

View File

@@ -178,13 +178,13 @@
"anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_TestSimpleTableWithEnum" },{ "$ref" : "#/definitions/MyGame_Example2_Monster" }] "anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_TestSimpleTableWithEnum" },{ "$ref" : "#/definitions/MyGame_Example2_Monster" }]
}, },
"test4" : { "test4" : {
"$ref" : "#/definitions/MyGame_Example_Test" "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Test"}
}, },
"testarrayofstring" : { "testarrayofstring" : {
"type" : "array", "items" : {"type" : "string"} "type" : "array", "items" : {"type" : "string"}
}, },
"testarrayoftables" : { "testarrayoftables" : {
"$ref" : "#/definitions/MyGame_Example_Monster" "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Monster"}
}, },
"enemy" : { "enemy" : {
"$ref" : "#/definitions/MyGame_Example_Monster" "$ref" : "#/definitions/MyGame_Example_Monster"
@@ -238,13 +238,13 @@
"type" : "array", "items" : {"type" : "string"} "type" : "array", "items" : {"type" : "string"}
}, },
"testarrayofsortedstruct" : { "testarrayofsortedstruct" : {
"$ref" : "#/definitions/MyGame_Example_Ability" "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Ability"}
}, },
"flex" : { "flex" : {
"type" : "array", "items" : {"type" : "integer", "minimum" : 0, "maximum" :255"} "type" : "array", "items" : {"type" : "integer", "minimum" : 0, "maximum" :255"}
}, },
"test5" : { "test5" : {
"$ref" : "#/definitions/MyGame_Example_Test" "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Test"}
}, },
"vector_of_longs" : { "vector_of_longs" : {
"type" : "array", "items" : {"type" : "integer", "minimum" : -9223372036854775808, "maximum" : 9223372036854775807} "type" : "array", "items" : {"type" : "integer", "minimum" : -9223372036854775808, "maximum" : 9223372036854775807}
@@ -256,7 +256,7 @@
"$ref" : "#/definitions/MyGame_InParentNamespace" "$ref" : "#/definitions/MyGame_InParentNamespace"
}, },
"vector_of_referrables" : { "vector_of_referrables" : {
"$ref" : "#/definitions/MyGame_Example_Referrable" "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Referrable"}
}, },
"single_weak_reference" : { "single_weak_reference" : {
"type" : "integer", "minimum" : 0, "maximum" : 18446744073709551615 "type" : "integer", "minimum" : 0, "maximum" : 18446744073709551615
@@ -265,7 +265,7 @@
"type" : "array", "items" : {"type" : "integer", "minimum" : 0, "maximum" : 18446744073709551615} "type" : "array", "items" : {"type" : "integer", "minimum" : 0, "maximum" : 18446744073709551615}
}, },
"vector_of_strong_referrables" : { "vector_of_strong_referrables" : {
"$ref" : "#/definitions/MyGame_Example_Referrable" "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Referrable"}
}, },
"co_owning_reference" : { "co_owning_reference" : {
"type" : "integer", "minimum" : 0, "maximum" : 18446744073709551615 "type" : "integer", "minimum" : 0, "maximum" : 18446744073709551615
@@ -292,7 +292,7 @@
"anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" }] "anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" }]
}, },
"vector_of_enums" : { "vector_of_enums" : {
"$ref" : "#/definitions/MyGame_Example_Color" "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Color"}
}, },
"signed_enum" : { "signed_enum" : {
"$ref" : "#/definitions/MyGame_Example_Race" "$ref" : "#/definitions/MyGame_Example_Race"