
com.reprezen.genflow.rapidml.jsonschema.XGenerateJsonSchema Maven / Gradle / Ivy
The newest version!
/**
* Copyright © 2013, 2016 Modelsolv, Inc.
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property
* of ModelSolv, Inc. See the file license.html in the root directory of
* this project for further information.
*/
package com.reprezen.genflow.rapidml.jsonschema;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.reprezen.genflow.api.template.IGenTemplateContext;
import com.reprezen.genflow.api.zenmodel.ZenModelOutputItem;
import com.reprezen.genflow.common.jsonschema.JsonSchemaFormat;
import com.reprezen.genflow.common.jsonschema.Options;
import com.reprezen.genflow.common.jsonschema.builder.JsonSchemaNodeFactory;
import com.reprezen.rapidml.ZenModel;
import java.util.ArrayList;
import java.util.Map;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Exceptions;
@SuppressWarnings("all")
public class XGenerateJsonSchema extends ZenModelOutputItem {
public static final String IS_SWAGGER_FORMAT_PARAM = "isSwaggerFormat";
private final ObjectMapper mapper = new ObjectMapper();
private final JsonSchemaNodeFactory jsonSchemaNodeFactory;
public XGenerateJsonSchema() {
this(JsonSchemaFormat.STANDARD);
}
public XGenerateJsonSchema(final JsonSchemaFormat schemaFormat) {
this(new JsonSchemaNodeFactory(schemaFormat));
}
public XGenerateJsonSchema(final JsonSchemaNodeFactory jsonSchemaNodeFactory) {
this.jsonSchemaNodeFactory = jsonSchemaNodeFactory;
}
@Override
public void init(final IGenTemplateContext context) {
super.init(context);
this.jsonSchemaNodeFactory.setOptions(Options.fromParams(context.getGenTargetParameters()));
}
@Override
public String generate(final ZenModel model) {
try {
final String result = this.mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this.getJsonSchemaNode(model));
return result;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
protected ObjectNode getJsonSchemaNode(final ZenModel model) {
final ObjectNode root = this.mapper.createObjectNode();
ObjectNode _put = root.put("$schema", "http://json-schema.org/draft-04/schema#");
StringConcatenation _builder = new StringConcatenation();
_builder.append("Schema for ");
String _name = model.getName();
_builder.append(_name);
_builder.append(" model");
_put.put("description", _builder.toString()).put("type", "object").put("title", model.getName());
final ObjectNode definitions = this.jsonSchemaNodeFactory.generateDefinitionsNode(model);
root.set("definitions", definitions);
final ObjectNode properties = root.putObject("properties");
ArrayList> _newArrayList = Lists.>newArrayList(definitions.fields());
for (final Map.Entry definition : _newArrayList) {
{
final String key = definition.getKey();
properties.putObject(key).put("$ref", ("#/definitions/" + key));
}
}
return root;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy