|
| 1 | +/* |
| 2 | + * Copyright 2020 the original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.github.hauner.openapi.micronaut.processor |
| 18 | + |
| 19 | +import com.github.hauner.openapi.core.framework.FrameworkAnnotation |
| 20 | +import com.github.hauner.openapi.core.framework.FrameworkAnnotations |
| 21 | +import com.github.hauner.openapi.core.model.HttpMethod |
| 22 | +import com.github.hauner.openapi.core.model.RequestBody |
| 23 | +import com.github.hauner.openapi.core.model.parameters.CookieParameter |
| 24 | +import com.github.hauner.openapi.core.model.parameters.HeaderParameter |
| 25 | +import com.github.hauner.openapi.core.model.parameters.MultipartParameter |
| 26 | +import com.github.hauner.openapi.core.model.parameters.Parameter |
| 27 | +import com.github.hauner.openapi.core.model.parameters.PathParameter |
| 28 | +import com.github.hauner.openapi.micronaut.model.parameters.QueryParameter |
| 29 | +import groovy.util.logging.Slf4j |
| 30 | + |
| 31 | +/** |
| 32 | + * provides Micronaut annotation details. |
| 33 | + * |
| 34 | + * @author Martin Hauner |
| 35 | + */ |
| 36 | +@Slf4j |
| 37 | +class MicronautFrameworkAnnotations implements FrameworkAnnotations { |
| 38 | + private static ANNOTATION_PKG = 'io.micronaut.http.annotation' |
| 39 | + |
| 40 | + def MAPPING_ANNOTATIONS = [ |
| 41 | + (HttpMethod.DELETE) : new FrameworkAnnotation (name: 'Delete', pkg: ANNOTATION_PKG), |
| 42 | + (HttpMethod.GET) : new FrameworkAnnotation (name: 'Get', pkg: ANNOTATION_PKG), |
| 43 | + (HttpMethod.HEAD) : new FrameworkAnnotation (name: 'Head', pkg: ANNOTATION_PKG), |
| 44 | + (HttpMethod.OPTIONS): new FrameworkAnnotation (name: 'Options', pkg: ANNOTATION_PKG), |
| 45 | + (HttpMethod.PATCH) : new FrameworkAnnotation (name: 'Patch', pkg: ANNOTATION_PKG), |
| 46 | + (HttpMethod.POST) : new FrameworkAnnotation (name: 'Post', pkg: ANNOTATION_PKG), |
| 47 | + (HttpMethod.PUT) : new FrameworkAnnotation (name: 'Put', pkg: ANNOTATION_PKG), |
| 48 | + (HttpMethod.TRACE) : new FrameworkAnnotation (name: 'Trace', pkg: ANNOTATION_PKG) |
| 49 | + ] |
| 50 | + |
| 51 | + def PARAMETER_ANNOTATIONS = [ |
| 52 | + query : new FrameworkAnnotation (name: 'QueryValue', pkg: ANNOTATION_PKG), |
| 53 | + header : new FrameworkAnnotation (name: 'Header', pkg: ANNOTATION_PKG), |
| 54 | + cookie : new FrameworkAnnotation (name: 'CookieValue', pkg: ANNOTATION_PKG), |
| 55 | + path : new FrameworkAnnotation (name: 'PathVariable', pkg: ANNOTATION_PKG), |
| 56 | + multipart: new FrameworkAnnotation (name: 'Part', pkg: ANNOTATION_PKG), |
| 57 | + body : new FrameworkAnnotation(name: 'Body', pkg: ANNOTATION_PKG) |
| 58 | + ] |
| 59 | + |
| 60 | + def UNKNOWN_ANNOTATION = new FrameworkAnnotation(name: 'Unknown', pkg: 'fix.me') |
| 61 | + |
| 62 | + @Override |
| 63 | + FrameworkAnnotation getAnnotation (HttpMethod httpMethod) { |
| 64 | + MAPPING_ANNOTATIONS[httpMethod] |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + FrameworkAnnotation getAnnotation (Parameter parameter) { |
| 69 | + switch (parameter) { |
| 70 | + case {it instanceof QueryParameter}: |
| 71 | + return PARAMETER_ANNOTATIONS.'query' |
| 72 | + case {it instanceof HeaderParameter}: |
| 73 | + return PARAMETER_ANNOTATIONS.header |
| 74 | + case {it instanceof CookieParameter}: |
| 75 | + return PARAMETER_ANNOTATIONS.cookie |
| 76 | + case {it instanceof PathParameter}: |
| 77 | + return PARAMETER_ANNOTATIONS.path |
| 78 | + case {it instanceof MultipartParameter}: |
| 79 | + return PARAMETER_ANNOTATIONS.multipart |
| 80 | + case {it instanceof RequestBody}: |
| 81 | + return PARAMETER_ANNOTATIONS.body |
| 82 | + default: |
| 83 | + log.error ("unknown parameter type: ${parameter.class.name}") |
| 84 | + return UNKNOWN_ANNOTATION |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments