• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "idl_gen_json_schema.h"
18 
19 #include <algorithm>
20 #include <iostream>
21 #include <limits>
22 
23 #include "flatbuffers/code_generators.h"
24 #include "flatbuffers/idl.h"
25 #include "flatbuffers/util.h"
26 
27 namespace flatbuffers {
28 
29 namespace jsons {
30 
31 namespace {
32 
GenFullName(const T * enum_def)33 template<class T> static std::string GenFullName(const T *enum_def) {
34   std::string full_name;
35   const auto &name_spaces = enum_def->defined_namespace->components;
36   for (auto ns = name_spaces.cbegin(); ns != name_spaces.cend(); ++ns) {
37     full_name.append(*ns + "_");
38   }
39   full_name.append(enum_def->name);
40   return full_name;
41 }
42 
GenTypeRef(const T * enum_def)43 template<class T> static std::string GenTypeRef(const T *enum_def) {
44   return "\"$ref\" : \"#/definitions/" + GenFullName(enum_def) + "\"";
45 }
46 
GenType(const std::string & name)47 static std::string GenType(const std::string &name) {
48   return "\"type\" : \"" + name + "\"";
49 }
50 
GenType(BaseType type)51 static std::string GenType(BaseType type) {
52   switch (type) {
53     case BASE_TYPE_BOOL: return "\"type\" : \"boolean\"";
54     case BASE_TYPE_CHAR:
55       return "\"type\" : \"integer\", \"minimum\" : " +
56              NumToString(std::numeric_limits<int8_t>::min()) +
57              ", \"maximum\" : " +
58              NumToString(std::numeric_limits<int8_t>::max());
59     case BASE_TYPE_UCHAR:
60       return "\"type\" : \"integer\", \"minimum\" : 0, \"maximum\" :" +
61              NumToString(std::numeric_limits<uint8_t>::max());
62     case BASE_TYPE_SHORT:
63       return "\"type\" : \"integer\", \"minimum\" : " +
64              NumToString(std::numeric_limits<int16_t>::min()) +
65              ", \"maximum\" : " +
66              NumToString(std::numeric_limits<int16_t>::max());
67     case BASE_TYPE_USHORT:
68       return "\"type\" : \"integer\", \"minimum\" : 0, \"maximum\" : " +
69              NumToString(std::numeric_limits<uint16_t>::max());
70     case BASE_TYPE_INT:
71       return "\"type\" : \"integer\", \"minimum\" : " +
72              NumToString(std::numeric_limits<int32_t>::min()) +
73              ", \"maximum\" : " +
74              NumToString(std::numeric_limits<int32_t>::max());
75     case BASE_TYPE_UINT:
76       return "\"type\" : \"integer\", \"minimum\" : 0, \"maximum\" : " +
77              NumToString(std::numeric_limits<uint32_t>::max());
78     case BASE_TYPE_LONG:
79       return "\"type\" : \"integer\", \"minimum\" : " +
80              NumToString(std::numeric_limits<int64_t>::min()) +
81              ", \"maximum\" : " +
82              NumToString(std::numeric_limits<int64_t>::max());
83     case BASE_TYPE_ULONG:
84       return "\"type\" : \"integer\", \"minimum\" : 0, \"maximum\" : " +
85              NumToString(std::numeric_limits<uint64_t>::max());
86     case BASE_TYPE_FLOAT:
87     case BASE_TYPE_DOUBLE: return "\"type\" : \"number\"";
88     case BASE_TYPE_STRING: return "\"type\" : \"string\"";
89     default: return "";
90   }
91 }
92 
GenBaseType(const Type & type)93 static std::string GenBaseType(const Type &type) {
94   if (type.struct_def != nullptr) { return GenTypeRef(type.struct_def); }
95   if (type.enum_def != nullptr) { return GenTypeRef(type.enum_def); }
96   return GenType(type.base_type);
97 }
98 
GenArrayType(const Type & type)99 static std::string GenArrayType(const Type &type) {
100   std::string element_type;
101   if (type.struct_def != nullptr) {
102     element_type = GenTypeRef(type.struct_def);
103   } else if (type.enum_def != nullptr) {
104     element_type = GenTypeRef(type.enum_def);
105   } else {
106     element_type = GenType(type.element);
107   }
108 
109   return "\"type\" : \"array\", \"items\" : {" + element_type + "}";
110 }
111 
GenType(const Type & type)112 static std::string GenType(const Type &type) {
113   switch (type.base_type) {
114     case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH();  // fall thru
115     case BASE_TYPE_VECTOR: {
116       return GenArrayType(type);
117     }
118     case BASE_TYPE_STRUCT: {
119       return GenTypeRef(type.struct_def);
120     }
121     case BASE_TYPE_UNION: {
122       std::string union_type_string("\"anyOf\": [");
123       const auto &union_types = type.enum_def->Vals();
124       for (auto ut = union_types.cbegin(); ut < union_types.cend(); ++ut) {
125         const auto &union_type = *ut;
126         if (union_type->union_type.base_type == BASE_TYPE_NONE) { continue; }
127         if (union_type->union_type.base_type == BASE_TYPE_STRUCT) {
128           union_type_string.append(
129               "{ " + GenTypeRef(union_type->union_type.struct_def) + " }");
130         }
131         if (union_type != *type.enum_def->Vals().rbegin()) {
132           union_type_string.append(",");
133         }
134       }
135       union_type_string.append("]");
136       return union_type_string;
137     }
138     case BASE_TYPE_UTYPE: return GenTypeRef(type.enum_def);
139     default: {
140       return GenBaseType(type);
141     }
142   }
143 }
144 
145 }  // namespace
146 
147 class JsonSchemaGenerator : public BaseGenerator {
148  private:
149   std::string code_;
150 
151  public:
JsonSchemaGenerator(const Parser & parser,const std::string & path,const std::string & file_name)152   JsonSchemaGenerator(const Parser &parser, const std::string &path,
153                       const std::string &file_name)
154       : BaseGenerator(parser, path, file_name, "", "", "json") {}
155 
JsonSchemaGenerator(const BaseGenerator & base_generator)156   explicit JsonSchemaGenerator(const BaseGenerator &base_generator)
157       : BaseGenerator(base_generator) {}
158 
GeneratedFileName(const std::string & path,const std::string & file_name,const IDLOptions & options) const159   std::string GeneratedFileName(const std::string &path,
160                                 const std::string &file_name,
161                                 const IDLOptions &options /* unused */) const {
162     (void)options;
163     return path + file_name + ".schema.json";
164   }
165 
166   // If indentation is less than 0, that indicates we don't want any newlines
167   // either.
NewLine() const168   std::string NewLine() const {
169     return parser_.opts.indent_step >= 0 ? "\n" : "";
170   }
171 
Indent(int indent) const172   std::string Indent(int indent) const {
173     const auto num_spaces = indent * std::max(parser_.opts.indent_step, 0);
174     return std::string(num_spaces, ' ');
175   }
176 
PrepareDescription(const std::vector<std::string> & comment_lines)177   std::string PrepareDescription(
178       const std::vector<std::string> &comment_lines) {
179     std::string comment;
180     for (auto line_iterator = comment_lines.cbegin();
181          line_iterator != comment_lines.cend(); ++line_iterator) {
182       const auto &comment_line = *line_iterator;
183 
184       // remove leading and trailing spaces from comment line
185       const auto start = std::find_if(comment_line.begin(), comment_line.end(),
186                                       [](char c) { return !isspace(c); });
187       const auto end =
188           std::find_if(comment_line.rbegin(), comment_line.rend(), [](char c) {
189             return !isspace(c);
190           }).base();
191       if (start < end) {
192         comment.append(start, end);
193       } else {
194         comment.append(comment_line);
195       }
196 
197       if (line_iterator + 1 != comment_lines.cend()) comment.append("\n");
198     }
199     if (!comment.empty()) {
200       std::string description;
201       if (EscapeString(comment.c_str(), comment.length(), &description, true,
202                        true)) {
203         return description;
204       }
205       return "";
206     }
207     return "";
208   }
209 
generate()210   bool generate() {
211     code_ = "";
212     if (parser_.root_struct_def_ == nullptr) {
213       std::cerr << "Error: Binary schema not generated, no root struct found\n";
214       return false;
215     }
216     code_ += "{" + NewLine();
217     code_ += Indent(1) +
218              "\"$schema\": \"https://json-schema.org/draft/2019-09/schema\"," +
219              NewLine();
220     code_ += Indent(1) + "\"definitions\": {" + NewLine();
221     for (auto e = parser_.enums_.vec.cbegin(); e != parser_.enums_.vec.cend();
222          ++e) {
223       code_ += Indent(2) + "\"" + GenFullName(*e) + "\" : {" + NewLine();
224       code_ += Indent(3) + GenType("string") + "," + NewLine();
225       auto enumdef(Indent(3) + "\"enum\": [");
226       for (auto enum_value = (*e)->Vals().begin();
227            enum_value != (*e)->Vals().end(); ++enum_value) {
228         enumdef.append("\"" + (*enum_value)->name + "\"");
229         if (*enum_value != (*e)->Vals().back()) { enumdef.append(", "); }
230       }
231       enumdef.append("]");
232       code_ += enumdef + NewLine();
233       code_ += Indent(2) + "}," + NewLine();  // close type
234     }
235     for (auto s = parser_.structs_.vec.cbegin();
236          s != parser_.structs_.vec.cend(); ++s) {
237       const auto &structure = *s;
238       code_ += Indent(2) + "\"" + GenFullName(structure) + "\" : {" + NewLine();
239       code_ += Indent(3) + GenType("object") + "," + NewLine();
240       const auto &comment_lines = structure->doc_comment;
241       auto comment = PrepareDescription(comment_lines);
242       if (comment != "") {
243         code_ += Indent(3) + "\"description\" : " + comment + "," + NewLine();
244       }
245 
246       code_ += Indent(3) + "\"properties\" : {" + NewLine();
247 
248       const auto &properties = structure->fields.vec;
249       for (auto prop = properties.cbegin(); prop != properties.cend(); ++prop) {
250         const auto &property = *prop;
251         std::string arrayInfo = "";
252         if (IsArray(property->value.type)) {
253           arrayInfo = "," + NewLine() + Indent(8) + "\"minItems\": " +
254                       NumToString(property->value.type.fixed_length) + "," +
255                       NewLine() + Indent(8) + "\"maxItems\": " +
256                       NumToString(property->value.type.fixed_length);
257         }
258         std::string deprecated_info = "";
259         if (property->deprecated) {
260           deprecated_info =
261               "," + NewLine() + Indent(8) + "\"deprecated\" : true";
262         }
263         std::string typeLine = Indent(4) + "\"" + property->name + "\"";
264         typeLine += " : {" + NewLine() + Indent(8);
265         typeLine += GenType(property->value.type);
266         typeLine += arrayInfo;
267         typeLine += deprecated_info;
268         auto description = PrepareDescription(property->doc_comment);
269         if (description != "") {
270           typeLine +=
271               "," + NewLine() + Indent(8) + "\"description\" : " + description;
272         }
273 
274         typeLine += NewLine() + Indent(7) + "}";
275         if (property != properties.back()) { typeLine.append(","); }
276         code_ += typeLine + NewLine();
277       }
278       code_ += Indent(3) + "}," + NewLine();  // close properties
279 
280       std::vector<FieldDef *> requiredProperties;
281       std::copy_if(properties.begin(), properties.end(),
282                    back_inserter(requiredProperties),
283                    [](FieldDef const *prop) { return prop->IsRequired(); });
284       if (!requiredProperties.empty()) {
285         auto required_string(Indent(3) + "\"required\" : [");
286         for (auto req_prop = requiredProperties.cbegin();
287              req_prop != requiredProperties.cend(); ++req_prop) {
288           required_string.append("\"" + (*req_prop)->name + "\"");
289           if (*req_prop != requiredProperties.back()) {
290             required_string.append(", ");
291           }
292         }
293         required_string.append("],");
294         code_ += required_string + NewLine();
295       }
296       code_ += Indent(3) + "\"additionalProperties\" : false" + NewLine();
297       auto closeType(Indent(2) + "}");
298       if (*s != parser_.structs_.vec.back()) { closeType.append(","); }
299       code_ += closeType + NewLine();  // close type
300     }
301     code_ += Indent(1) + "}," + NewLine();  // close definitions
302 
303     // mark root type
304     code_ += Indent(1) + "\"$ref\" : \"#/definitions/" +
305              GenFullName(parser_.root_struct_def_) + "\"" + NewLine();
306 
307     code_ += "}" + NewLine();  // close schema root
308     return true;
309   }
310 
save() const311   bool save() const {
312     const auto file_path = GeneratedFileName(path_, file_name_, parser_.opts);
313     return SaveFile(file_path.c_str(), code_, false);
314   }
315 
getJson()316   const std::string getJson() { return code_; }
317 };
318 }  // namespace jsons
319 
GenerateJsonSchema(const Parser & parser,const std::string & path,const std::string & file_name)320 static bool GenerateJsonSchema(const Parser &parser, const std::string &path,
321                                const std::string &file_name) {
322   jsons::JsonSchemaGenerator generator(parser, path, file_name);
323   if (!generator.generate()) { return false; }
324   return generator.save();
325 }
326 
327 namespace {
328 
329 class JsonSchemaCodeGenerator : public CodeGenerator {
330  public:
GenerateCode(const Parser & parser,const std::string & path,const std::string & filename)331   Status GenerateCode(const Parser &parser, const std::string &path,
332                       const std::string &filename) override {
333     if (!GenerateJsonSchema(parser, path, filename)) { return Status::ERROR; }
334     return Status::OK;
335   }
336 
GenerateCode(const uint8_t *,int64_t,const CodeGenOptions &)337   Status GenerateCode(const uint8_t *, int64_t,
338                       const CodeGenOptions &) override {
339     return Status::NOT_IMPLEMENTED;
340   }
341 
GenerateMakeRule(const Parser & parser,const std::string & path,const std::string & filename,std::string & output)342   Status GenerateMakeRule(const Parser &parser, const std::string &path,
343                           const std::string &filename,
344                           std::string &output) override {
345     (void)parser;
346     (void)path;
347     (void)filename;
348     (void)output;
349     return Status::NOT_IMPLEMENTED;
350   }
351 
GenerateGrpcCode(const Parser & parser,const std::string & path,const std::string & filename)352   Status GenerateGrpcCode(const Parser &parser, const std::string &path,
353                           const std::string &filename) override {
354     (void)parser;
355     (void)path;
356     (void)filename;
357     return Status::NOT_IMPLEMENTED;
358   }
359 
GenerateRootFile(const Parser & parser,const std::string & path)360   Status GenerateRootFile(const Parser &parser,
361                           const std::string &path) override {
362     (void)parser;
363     (void)path;
364     return Status::NOT_IMPLEMENTED;
365   }
IsSchemaOnly() const366   bool IsSchemaOnly() const override { return true; }
367 
SupportsBfbsGeneration() const368   bool SupportsBfbsGeneration() const override { return false; }
369 
SupportsRootFileGeneration() const370   bool SupportsRootFileGeneration() const override { return false; }
371 
Language() const372   IDLOptions::Language Language() const override {
373     return IDLOptions::kJsonSchema;
374   }
375 
LanguageName() const376   std::string LanguageName() const override { return "JsonSchema"; }
377 };
378 }  // namespace
379 
NewJsonSchemaCodeGenerator()380 std::unique_ptr<CodeGenerator> NewJsonSchemaCodeGenerator() {
381   return std::unique_ptr<JsonSchemaCodeGenerator>(
382       new JsonSchemaCodeGenerator());
383 }
384 }  // namespace flatbuffers
385