-
Notifications
You must be signed in to change notification settings - Fork 10
/
Swagger4jaxrsGrailsPlugin.groovy
99 lines (80 loc) · 3.29 KB
/
Swagger4jaxrsGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import grails.util.Environment
import org.codehaus.groovy.grails.commons.GrailsApplication
import com.wordnik.swagger.jaxrs.config.BeanConfig
class Swagger4jaxrsGrailsPlugin {
def version = "0.3-SNAPSHOT"
def grailsVersion = "2.0 > *"
def pluginExcludes = [
"web-app/WEB-INF/**"
]
def title = "Swagger for JAX-RS Plugin"
def description = 'Adds Swagger support to document REST APIs of projects that use the JAX-RS (JSR 311) plugin'
def documentation = "https://github.com/nerdErg/swagger4jaxrs"
def license = "APACHE"
def organization = [ name: "nerdErg Pty. Ltd.", url: "http://www.nerderg.com/" ]
def developers = [
[ name: "Angel Ruiz", email: "[email protected]" ],
[ name: "Aaron Brown", email: "[email protected]"],
]
def issueManagement = [ system: "GitHub", url: "https://github.com/nerdErg/swagger4jaxrs/issues" ]
def scm = [ url: "https://github.com/nerdErg/swagger4jaxrs" ]
def dependsOn = [
jaxrs: "0.8 > *"
]
def loadAfter = [
"jaxrs"
]
def doWithSpring = {
mergeConfig(application)
ConfigObject local = application.config.'swagger4jaxrs'
validateLocalConfig(local)
swaggerConfig(BeanConfig) { bean ->
bean.autowire = true
resourcePackage = local.resourcePackage
basePath = local.basePath ?: ''
version = local.version ?: '1'
title = local.title ?: grails.util.Metadata.current.'app.name'
description = local.description ?: ''
contact = local.contact ?: ''
license = local.license ?: ''
licenseUrl = local.licenseUrl ?: ''
scan = local.scan ?: true
}
}
def onConfigChange = { event ->
mergeConfig(application)
ConfigObject local = application.config.'swagger4jaxrs'
validateLocalConfig(local)
event.ctx.getBean('swaggerConfig').with {
resourcePackage = local.resourcePackage
basePath = local.basePath ?: ''
version = local.version ?: "1"
title = local.title ?: grails.util.Metadata.current.'app.name'
description = local.description ?: ""
contact = local.contact ?: ""
license = local.license ?: ""
licenseUrl = local.licenseUrl ?: ""
scan = local.scan ?: true
}
}
/**
* Merges plugin config with host app config, but allowing customization
* @param app
*/
private void mergeConfig(GrailsApplication app) {
ConfigObject currentConfig = app.config.org.grails.jaxrs
ConfigSlurper slurper = new ConfigSlurper(Environment.current.name)
ConfigObject secondaryConfig = slurper.parse(app.classLoader.loadClass("SwaggerConfig"))
ConfigObject config = new ConfigObject()
config.putAll(secondaryConfig.org.grails.jaxrs.merge(currentConfig))
app.config.org.grails.jaxrs = config
}
private void validateLocalConfig(ConfigObject local) {
if (local.isEmpty()) {
throw new IllegalStateException("The swagger4jaxrs config is missing.")
}
if (local.resourcePackage.isEmpty()) {
throw new IllegalStateException("The swagger4jaxrs config requires a resourcePackage path.")
}
}
}