Jsonschema add prop comments (#6617)

* Add comments for properties

* regenerated test files

* Reverted changes in testfiles generated with generate_code.bat on windows

* fixed formatting in util.cpp because of failed .travis/format_check.sh

* removed the use of tuple

* util.cpp: Fixed format

* idl_gen_json_schema.cpp: removed extra comma in deprecated fields

* Corrected monster_test.schema.json

* idl_gen_json_schema: Inlined Trim function

* Fixed format

* PrepareDescription(): Avoid unnecessary string copy

* fixed formatting
This commit is contained in:
schoetbi
2021-06-21 20:42:07 +02:00
committed by GitHub
parent 962751a6ec
commit c8db1ca5d4
2 changed files with 47 additions and 17 deletions

View File

@@ -164,6 +164,38 @@ class JsonSchemaGenerator : public BaseGenerator {
return std::string(num_spaces, ' '); return std::string(num_spaces, ' ');
} }
std::string PrepareDescription(
const std::vector<std::string> &comment_lines) {
std::string comment;
for (auto line_iterator = comment_lines.cbegin();
line_iterator != comment_lines.cend(); ++line_iterator) {
const auto &comment_line = *line_iterator;
// remove leading and trailing spaces from comment line
const auto start = std::find_if(comment_line.begin(), comment_line.end(),
[](char c) { return !isspace(c); });
const auto end = std::find_if(comment_line.rbegin(), comment_line.rend(),
[](char c) { return !isspace(c); })
.base();
if (start < end) {
comment.append(start, end);
} else {
comment.append(comment_line);
}
if (line_iterator + 1 != comment_lines.cend()) comment.append("\n");
}
if (!comment.empty()) {
std::string description;
if (EscapeString(comment.c_str(), comment.length(), &description, true,
true)) {
return description;
}
return "";
}
return "";
}
bool generate() { bool generate() {
code_ = ""; code_ = "";
if (parser_.root_struct_def_ == nullptr) { return false; } if (parser_.root_struct_def_ == nullptr) { return false; }
@@ -191,21 +223,12 @@ class JsonSchemaGenerator : public BaseGenerator {
const auto &structure = *s; const auto &structure = *s;
code_ += Indent(2) + "\"" + GenFullName(structure) + "\" : {" + NewLine(); code_ += Indent(2) + "\"" + GenFullName(structure) + "\" : {" + NewLine();
code_ += Indent(3) + GenType("object") + "," + NewLine(); code_ += Indent(3) + GenType("object") + "," + NewLine();
std::string comment;
const auto &comment_lines = structure->doc_comment; const auto &comment_lines = structure->doc_comment;
for (auto comment_line = comment_lines.cbegin(); auto comment = PrepareDescription(comment_lines);
comment_line != comment_lines.cend(); ++comment_line) { if (comment != "") {
comment.append(*comment_line); code_ += Indent(3) + "\"description\" : " + comment + "," + NewLine();
}
if (!comment.empty()) {
std::string description;
if (!EscapeString(comment.c_str(), comment.length(), &description, true,
true)) {
return false;
}
code_ +=
Indent(3) + "\"description\" : " + description + "," + NewLine();
} }
code_ += Indent(3) + "\"properties\" : {" + NewLine(); code_ += Indent(3) + "\"properties\" : {" + NewLine();
const auto &properties = structure->fields.vec; const auto &properties = structure->fields.vec;
@@ -221,13 +244,19 @@ class JsonSchemaGenerator : public BaseGenerator {
std::string deprecated_info = ""; std::string deprecated_info = "";
if (property->deprecated) { if (property->deprecated) {
deprecated_info = deprecated_info =
"," + NewLine() + Indent(8) + "\"deprecated\" : true,"; "," + 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);
typeLine += GenType(property->value.type); typeLine += GenType(property->value.type);
typeLine += arrayInfo; typeLine += arrayInfo;
typeLine += deprecated_info; typeLine += deprecated_info;
auto description = PrepareDescription(property->doc_comment);
if (description != "") {
typeLine +=
"," + NewLine() + Indent(8) + "\"description\" : " + description;
}
typeLine += NewLine() + Indent(7) + "}"; typeLine += NewLine() + Indent(7) + "}";
if (property != properties.back()) { typeLine.append(","); } if (property != properties.back()) { typeLine.append(","); }
code_ += typeLine + NewLine(); code_ += typeLine + NewLine();

View File

@@ -162,7 +162,7 @@
}, },
"MyGame_Example_Monster" : { "MyGame_Example_Monster" : {
"type" : "object", "type" : "object",
"description" : " an example documentation comment: \"monster object\"", "description" : "an example documentation comment: \"monster object\"",
"properties" : { "properties" : {
"pos" : { "pos" : {
"$ref" : "#/definitions/MyGame_Example_Vec3" "$ref" : "#/definitions/MyGame_Example_Vec3"
@@ -178,7 +178,7 @@
}, },
"friendly" : { "friendly" : {
"type" : "boolean", "type" : "boolean",
"deprecated" : true, "deprecated" : true
}, },
"inventory" : { "inventory" : {
"type" : "array", "items" : {"type" : "integer", "minimum" : 0, "maximum" :255} "type" : "array", "items" : {"type" : "integer", "minimum" : 0, "maximum" :255}
@@ -199,7 +199,8 @@
"type" : "array", "items" : {"type" : "string"} "type" : "array", "items" : {"type" : "string"}
}, },
"testarrayoftables" : { "testarrayoftables" : {
"type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Monster"} "type" : "array", "items" : {"$ref" : "#/definitions/MyGame_Example_Monster"},
"description" : "an example documentation comment: this will end up in the generated code\nmultiline too"
}, },
"enemy" : { "enemy" : {
"$ref" : "#/definitions/MyGame_Example_Monster" "$ref" : "#/definitions/MyGame_Example_Monster"