@@ -11,6 +11,7 @@ import io.javalin.openapi.OpenApiContent
1111import io.javalin.openapi.OpenApiParam
1212import io.javalin.openapi.OpenApis
1313import io.javalin.openapi.getFormattedPath
14+ import io.javalin.openapi.processor.OpenApiAnnotationProcessor.Companion.trees
1415import io.javalin.openapi.processor.OpenApiGenerator.In.COOKIE
1516import io.javalin.openapi.processor.OpenApiGenerator.In.FORM_DATA
1617import io.javalin.openapi.processor.OpenApiGenerator.In.HEADER
@@ -29,6 +30,7 @@ import io.swagger.v3.parser.OpenAPIV3Parser
2930import io.swagger.v3.parser.core.models.ParseOptions
3031import java.util.TreeMap
3132import javax.annotation.processing.RoundEnvironment
33+ import javax.lang.model.element.Element
3234import javax.lang.model.type.TypeMirror
3335import javax.tools.Diagnostic
3436import javax.tools.Diagnostic.Kind.WARNING
@@ -39,13 +41,18 @@ internal class OpenApiGenerator {
3941
4042 fun generate (roundEnvironment : RoundEnvironment ) {
4143 val aggregatedOpenApiAnnotations = roundEnvironment.getElementsAnnotatedWith(OpenApis ::class .java)
42- .flatMap { it.getAnnotation(OpenApis ::class .java).value.asSequence() }
44+ .flatMap { element ->
45+ element.getAnnotation(OpenApis ::class .java)
46+ .value
47+ .asSequence()
48+ .map { element to it }
49+ }
4350
4451 val standaloneOpenApiAnnotations = roundEnvironment.getElementsAnnotatedWith(OpenApi ::class .java)
45- .map { it.getAnnotation(OpenApi ::class .java) }
52+ .map { it to it .getAnnotation(OpenApi ::class .java) }
4653
4754 val openApiAnnotationsByVersion = (aggregatedOpenApiAnnotations + standaloneOpenApiAnnotations)
48- .flatMap { it.versions.map { version -> version to it } }
55+ .flatMap { it.second. versions.map { version -> version to it } }
4956 .groupBy { (version, _) -> version }
5057 .mapValues { (_, annotations) -> annotations.map { it.second } }
5158
@@ -82,7 +89,7 @@ internal class OpenApiGenerator {
8289 * @param openApiAnnotations annotation instances to map
8390 * @return OpenApi JSON response
8491 */
85- private fun generateSchema (openApiAnnotations : Collection <OpenApi >): String {
92+ private fun generateSchema (openApiAnnotations : Collection <Pair < Element , OpenApi > >): String {
8693 val openApi = JsonObject ()
8794 openApi.addProperty(" openapi" , " 3.0.3" )
8895
@@ -96,7 +103,7 @@ internal class OpenApiGenerator {
96103 val paths = JsonObject ()
97104 openApi.add(" paths" , paths)
98105
99- for (routeAnnotation in openApiAnnotations.sortedBy { it.getFormattedPath() }) {
106+ for ((openApiElement, routeAnnotation) in openApiAnnotations.sortedBy { it.second .getFormattedPath() }) {
100107 if (routeAnnotation.ignore) {
101108 continue
102109 }
@@ -146,7 +153,7 @@ internal class OpenApiGenerator {
146153 val requestBodyAnnotation = routeAnnotation.requestBody
147154 val requestBody = JsonObject ()
148155 requestBody.addString(" description" , requestBodyAnnotation.description)
149- requestBody.addContent(requestBodyAnnotation.content)
156+ requestBody.addContent(openApiElement, requestBodyAnnotation.content)
150157 if (requestBody.size() > 0 ) {
151158 operation.add(" requestBody" , requestBody)
152159 }
@@ -166,7 +173,7 @@ internal class OpenApiGenerator {
166173 ?.let { HttpStatus .forStatus(it) }?.message
167174
168175 response.addString(" description" , description)
169- response.addContent(responseAnnotation.content)
176+ response.addContent(openApiElement, responseAnnotation.content)
170177 responses.add(responseAnnotation.status, response)
171178 }
172179
@@ -263,7 +270,7 @@ internal class OpenApiGenerator {
263270 return parameter
264271 }
265272
266- private fun JsonObject.addContent (contentAnnotations : Array <OpenApiContent >) {
273+ private fun JsonObject.addContent (element : Element , contentAnnotations : Array <OpenApiContent >) {
267274 val requestBodyContent = JsonObject ()
268275 val requestBodySchemes = TreeMap <String , JsonObject >()
269276
@@ -287,7 +294,21 @@ internal class OpenApiGenerator {
287294 }
288295
289296 if (mimeType == null ) {
290- OpenApiAnnotationProcessor .messager.printMessage(WARNING , " Cannot add $contentAnnotation , OpenApi generator cannot find matching mime type" )
297+ val compilationUnit = trees.getPath(element).compilationUnit
298+ val tree = trees.getTree(element)
299+ val startPosition = trees.sourcePositions.getStartPosition(compilationUnit, tree)
300+
301+ OpenApiAnnotationProcessor .messager.printMessage(
302+ WARNING ,
303+ """
304+ OpenApi generator cannot find matching mime type defined.
305+ Source:
306+ Annotation in ${compilationUnit.lineMap.getLineNumber(startPosition)} at ${compilationUnit.sourceFile.name} line
307+ Annotation:
308+ $contentAnnotation
309+ """ .trimIndent()
310+ )
311+
291312 continue
292313 }
293314
0 commit comments