com.reprezen.genflow.openapi.swaggerui.v3.XGenerateSwaggerUIv3.xtend Maven / Gradle / Ivy
/*******************************************************************************
* 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.openapi.swaggerui.v3
import com.fasterxml.jackson.annotation.JsonInclude.Include
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.google.common.base.Strings
import com.reprezen.genflow.api.normal.openapi.OpenApiNormalizer
import com.reprezen.genflow.api.normal.openapi.Option
import com.reprezen.genflow.api.template.IGenTemplateContext
import com.reprezen.genflow.common.HtmlInjections
import java.io.File
import static com.reprezen.genflow.common.HtmlInjections.*
class XGenerateSwaggerUIv3 {
extension HtmlInjections htmlInjections
val static mapper = if (Boolean.valueOf(true)) { // trick to get static code block
val m = new ObjectMapper();
m.setSerializationInclusion(Include.NON_NULL);
m;
}
def generate(String json, String uriPrefix, String version, IGenTemplateContext context) {
generate(json, uriPrefix, false, SwaggerUi3Options.DEFAULT, context);
}
def String generateNormalized(String modelText, File inputFile, Integer modelVersion, String uriPrefix,
boolean isLiveView, IGenTemplateContext context) {
val normalized = new OpenApiNormalizer(modelVersion, Option.MINIMAL_OPTIONS).of(modelText).normalizeToJson(
inputFile.toURI().toURL());
return generate(mapper.writeValueAsString(normalized), uriPrefix, isLiveView, SwaggerUi3Options.DEFAULT, context);
}
def String generate(String modelText, String uriPrefix, boolean isLiveView, SwaggerUi3Options options, IGenTemplateContext context) {
htmlInjections = context.genTargetParameters.get(HTML_INJECTIONS_PARAM) as HtmlInjections ?: new HtmlInjections
val json = modelText.toJson
'''
«HEAD_TOP.inject»
Swagger UI
«HEAD_BOTTOM.inject»
«BODY_TOP.inject»
«IF !isLiveView»
«ENDIF»
«IF isLiveView»
«ENDIF»
«BODY_BOTTOM.inject»
'''
}
val static ObjectMapper jsonMapper = new ObjectMapper()
val static ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory())
def private toJson(String modelText) {
if (modelText.trim().startsWith("{")) {
return modelText;
}
val tree = yamlMapper.readTree(modelText);
return jsonMapper.writeValueAsString(tree);
}
private def maybeFunction(String option) {
// apisSorter and methodsSorter options take either fixed string values (e.g. "alpha") or a Javascript function object.
// The latter should not be quoted, while the former should. This method applies quoting unless the value begins with "function".
if(option.startsWith("function")) option else '''"«option»"'''
}
}