2 * Copyright 2014 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "flatbuffers/code_generators.h"
19 #include "flatbuffers/idl.h"
20 #include "flatbuffers/util.h"
22 namespace flatbuffers {
24 static std::string GeneratedFileName(const std::string &path,
25 const std::string &file_name) {
26 return path + file_name + ".schema.json";
31 std::string GenNativeType(BaseType type) {
33 case BASE_TYPE_BOOL: return "boolean";
37 case BASE_TYPE_USHORT:
43 case BASE_TYPE_DOUBLE: return "number";
44 case BASE_TYPE_STRING: return "string";
45 case BASE_TYPE_ARRAY: return "array";
50 template<class T> std::string GenFullName(const T *enum_def) {
51 std::string full_name;
52 const auto &name_spaces = enum_def->defined_namespace->components;
53 for (auto ns = name_spaces.cbegin(); ns != name_spaces.cend(); ++ns) {
54 full_name.append(*ns + "_");
56 full_name.append(enum_def->name);
60 template<class T> std::string GenTypeRef(const T *enum_def) {
61 return "\"$ref\" : \"#/definitions/" + GenFullName(enum_def) + "\"";
64 std::string GenType(const std::string &name) {
65 return "\"type\" : \"" + name + "\"";
68 std::string GenType(const Type &type) {
69 if (type.enum_def != nullptr && !type.enum_def->is_union) {
70 // it is a reference to an enum type
71 return GenTypeRef(type.enum_def);
73 switch (type.base_type) {
74 case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru
75 case BASE_TYPE_VECTOR: {
77 typeline.append("\"type\" : \"array\", \"items\" : { ");
78 if (type.element == BASE_TYPE_STRUCT) {
79 typeline.append(GenTypeRef(type.struct_def));
81 typeline.append(GenType(GenNativeType(type.element)));
83 typeline.append(" }");
86 case BASE_TYPE_STRUCT: {
87 return GenTypeRef(type.struct_def);
89 case BASE_TYPE_UNION: {
90 std::string union_type_string("\"anyOf\": [");
91 const auto &union_types = type.enum_def->Vals();
92 for (auto ut = union_types.cbegin(); ut < union_types.cend(); ++ut) {
93 auto &union_type = *ut;
94 if (union_type->union_type.base_type == BASE_TYPE_NONE) { continue; }
95 if (union_type->union_type.base_type == BASE_TYPE_STRUCT) {
96 union_type_string.append(
97 "{ " + GenTypeRef(union_type->union_type.struct_def) + " }");
99 if (union_type != *type.enum_def->Vals().rbegin()) {
100 union_type_string.append(",");
103 union_type_string.append("]");
104 return union_type_string;
106 case BASE_TYPE_UTYPE: return GenTypeRef(type.enum_def);
107 default: return GenType(GenNativeType(type.base_type));
111 class JsonSchemaGenerator : public BaseGenerator {
116 JsonSchemaGenerator(const Parser &parser, const std::string &path,
117 const std::string &file_name)
118 : BaseGenerator(parser, path, file_name, "", "") {}
120 explicit JsonSchemaGenerator(const BaseGenerator &base_generator)
121 : BaseGenerator(base_generator) {}
126 code_ += " \"$schema\": \"http://json-schema.org/draft-04/schema#\",";
127 code_ += " \"definitions\": {";
128 for (auto e = parser_.enums_.vec.cbegin(); e != parser_.enums_.vec.cend();
130 code_ += " \"" + GenFullName(*e) + "\" : {";
131 code_ += " " + GenType("string") + ",";
132 std::string enumdef(" \"enum\": [");
133 for (auto enum_value = (*e)->Vals().begin();
134 enum_value != (*e)->Vals().end(); ++enum_value) {
135 enumdef.append("\"" + (*enum_value)->name + "\"");
136 if (*enum_value != (*e)->Vals().back()) { enumdef.append(", "); }
140 code_ += " },"; // close type
142 for (auto s = parser_.structs_.vec.cbegin();
143 s != parser_.structs_.vec.cend(); ++s) {
144 const auto &structure = *s;
145 code_ += " \"" + GenFullName(structure) + "\" : {";
146 code_ += " " + GenType("object") + ",";
148 const auto &comment_lines = structure->doc_comment;
149 for (auto comment_line = comment_lines.cbegin();
150 comment_line != comment_lines.cend(); ++comment_line) {
151 comment.append(*comment_line);
153 if (comment.size() > 0) {
154 code_ += " \"description\" : \"" + comment + "\",";
156 code_ += " \"properties\" : {";
158 const auto &properties = structure->fields.vec;
159 for (auto prop = properties.cbegin(); prop != properties.cend(); ++prop) {
160 const auto &property = *prop;
161 std::string arrayInfo = "";
162 if (IsArray(property->value.type)) {
163 arrayInfo = ",\n \"minItems\": " +
164 NumToString(property->value.type.fixed_length) +
165 ",\n \"maxItems\": " +
166 NumToString(property->value.type.fixed_length);
168 std::string typeLine =
169 " \"" + property->name + "\" : {\n" + " " +
170 GenType(property->value.type) + arrayInfo + "\n }";
171 if (property != properties.back()) { typeLine.append(","); }
174 code_ += " },"; // close properties
176 std::vector<FieldDef *> requiredProperties;
177 std::copy_if(properties.begin(), properties.end(),
178 back_inserter(requiredProperties),
179 [](FieldDef const *prop) { return prop->required; });
180 if (requiredProperties.size() > 0) {
181 std::string required_string(" \"required\" : [");
182 for (auto req_prop = requiredProperties.cbegin();
183 req_prop != requiredProperties.cend(); ++req_prop) {
184 required_string.append("\"" + (*req_prop)->name + "\"");
185 if (*req_prop != requiredProperties.back()) {
186 required_string.append(", ");
189 required_string.append("],");
190 code_ += required_string;
192 code_ += " \"additionalProperties\" : false";
193 std::string closeType(" }");
194 if (*s != parser_.structs_.vec.back()) { closeType.append(","); }
195 code_ += closeType; // close type
197 code_ += " },"; // close definitions
200 code_ += " \"$ref\" : \"#/definitions/" +
201 GenFullName(parser_.root_struct_def_) + "\"";
203 code_ += "}"; // close schema root
204 const std::string file_path = GeneratedFileName(path_, file_name_);
205 const std::string final_code = code_.ToString();
206 return SaveFile(file_path.c_str(), final_code, false);
211 bool GenerateJsonSchema(const Parser &parser, const std::string &path,
212 const std::string &file_name) {
213 jsons::JsonSchemaGenerator generator(parser, path, file_name);
214 return generator.generate();
216 } // namespace flatbuffers