com.reprezen.genflow.openapi.swaggerui.v2.XGenerateSwaggerUI.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.v2
import com.fasterxml.jackson.annotation.JsonInclude.Include
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.common.base.Strings
import com.reprezen.genflow.api.template.IGenTemplateContext
import com.reprezen.genflow.common.HtmlInjections
import io.swagger.models.Swagger
import java.net.URL
class XGenerateSwaggerUI {
val static mapper = if (Boolean.valueOf(true)) { // trick to get static code block
val m = new ObjectMapper
m.setSerializationInclusion(Include.NON_NULL)
m
}
def String generateForSwaggerSpec(Swagger swagger, String urlPrefix, URL resolutionBase, boolean isLiveView,
SwaggerUiOptions options, IGenTemplateContext context) {
val spec = mapper.writeValueAsString(swagger)
generateForSwaggerSpec(spec, urlPrefix, resolutionBase, isLiveView, options, context)
}
extension HtmlInjections htmlInjections
def String generateForSwaggerSpec(String spec, String urlPrefix, URL resolutionBase, boolean isLiveView,
SwaggerUiOptions options, IGenTemplateContext context) {
htmlInjections = context.genTargetParameters.get(HtmlInjections.HTML_INJECTIONS_PARAM) as HtmlInjections ?:
new HtmlInjections
'''
«HtmlInjections.HEAD_TOP.inject»
Swagger UI
«IF options.theme !== null»
«ENDIF»
«HtmlInjections.HEAD_BOTTOM.inject»
«HtmlInjections.BODY_TOP.inject»
«HtmlInjections.BODY_BOTTOM.inject»
'''
}
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»"'''
}
}