You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by il...@apache.org on 2015/12/04 10:27:51 UTC

[2/3] cxf git commit: Improving bean validation annotations support for @MatrixPatam and @BeanParam fields

Improving bean validation annotations support for @MatrixPatam and @BeanParam fields


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8a765e0b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8a765e0b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8a765e0b

Branch: refs/heads/master
Commit: 8a765e0b49200ccc5bab5a5290e826b15528899f
Parents: 4eede5f
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Fri Dec 4 10:16:11 2015 +0100
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Fri Dec 4 10:16:35 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/swagger/JaxRs2Extension.java      | 85 +++++++++-----------
 1 file changed, 37 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/8a765e0b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/JaxRs2Extension.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/JaxRs2Extension.java b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/JaxRs2Extension.java
index b89a5e9..87e0cf2 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/JaxRs2Extension.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/JaxRs2Extension.java
@@ -46,8 +46,8 @@ import io.swagger.converter.ModelConverters;
 import io.swagger.jaxrs.ext.AbstractSwaggerExtension;
 import io.swagger.jaxrs.ext.SwaggerExtension;
 import io.swagger.jaxrs.ext.SwaggerExtensions;
+import io.swagger.models.parameters.AbstractSerializableParameter;
 import io.swagger.models.parameters.Parameter;
-import io.swagger.models.properties.AbstractNumericProperty;
 import io.swagger.models.properties.ArrayProperty;
 import io.swagger.models.properties.Property;
 import io.swagger.models.properties.RefProperty;
@@ -80,6 +80,7 @@ public class JaxRs2Extension extends AbstractSwaggerExtension {
                 if (schema != null) {
                     mp.setProperty(schema);
                 }
+                applyBeanValidatorAnnotations(mp, annotations);
                 parameters.add(mp);
             } else if (annotation instanceof BeanParam) {
                 // Use Jackson's logic for processing Beans
@@ -163,72 +164,60 @@ public class JaxRs2Extension extends AbstractSwaggerExtension {
     /**
      * This is essentially a duplicate of {@link io.swagger.jackson.ModelResolver.applyBeanValidatorAnnotations}.
      *
-     * @param property
+     * @param parameter
      * @param annotations
      */
-    private void applyBeanValidatorAnnotations(final Parameter property, final List<Annotation> annotations) {
+    private void applyBeanValidatorAnnotations(final Parameter parameter, final List<Annotation> annotations) {
         Map<String, Annotation> annos = new HashMap<>();
         if (annotations != null) {
             for (Annotation annotation : annotations) {
                 annos.put(annotation.annotationType().getName(), annotation);
             }
         }
+
         if (annos.containsKey(NotNull.class.getName())) {
-            property.setRequired(true);
-        }
-        if (annos.containsKey(Min.class.getName()) && property instanceof AbstractNumericProperty) {
-            Min min = (Min) annos.get(Min.class.getName());
-            AbstractNumericProperty ap = (AbstractNumericProperty) property;
-            ap.setMinimum(new Double(min.value()));
-        }
-        if (annos.containsKey(Max.class.getName()) && property instanceof AbstractNumericProperty) {
-            Max max = (Max) annos.get(Max.class.getName());
-            AbstractNumericProperty ap = (AbstractNumericProperty) property;
-            ap.setMaximum(new Double(max.value()));
+            parameter.setRequired(true);
         }
-        if (annos.containsKey(Size.class.getName())) {
-            Size size = (Size) annos.get(Size.class.getName());
-            if (property instanceof AbstractNumericProperty) {
-                AbstractNumericProperty ap = (AbstractNumericProperty) property;
-                ap.setMinimum(new Double(size.min()));
-                ap.setMaximum(new Double(size.max()));
-            } else if (property instanceof StringProperty) {
-                StringProperty sp = (StringProperty) property;
-                sp.minLength(size.min());
-                sp.maxLength(size.max());
-            } else if (property instanceof ArrayProperty) {
-                ArrayProperty sp = (ArrayProperty) property;
-                sp.setMinItems(size.min());
-                sp.setMaxItems(size.max());
+
+        if (parameter instanceof AbstractSerializableParameter) {
+            AbstractSerializableParameter<?> serializable = (AbstractSerializableParameter<?>) parameter;
+
+            if (annos.containsKey(Min.class.getName())) {
+                Min min = (Min) annos.get(Min.class.getName());
+                serializable.setMinimum(new Double(min.value()));
             }
-        }
-        if (annos.containsKey(DecimalMin.class.getName())) {
-            DecimalMin min = (DecimalMin) annos.get(DecimalMin.class.getName());
-            if (property instanceof AbstractNumericProperty) {
-                AbstractNumericProperty ap = (AbstractNumericProperty) property;
+            if (annos.containsKey(Max.class.getName())) {
+                Max max = (Max) annos.get(Max.class.getName());
+                serializable.setMaximum(new Double(max.value()));
+            }
+            if (annos.containsKey(Size.class.getName())) {
+                Size size = (Size) annos.get(Size.class.getName());
+
+                serializable.setMinimum(new Double(size.min()));
+                serializable.setMaximum(new Double(size.max()));
+
+                serializable.setMinItems(size.min());
+                serializable.setMaxItems(size.max());
+            }
+            if (annos.containsKey(DecimalMin.class.getName())) {
+                DecimalMin min = (DecimalMin) annos.get(DecimalMin.class.getName());
                 if (min.inclusive()) {
-                    ap.setMinimum(new Double(min.value()));
+                    serializable.setMinimum(new Double(min.value()));
                 } else {
-                    ap.setExclusiveMinimum(!min.inclusive());
+                    serializable.setExclusiveMinimum(!min.inclusive());
                 }
             }
-        }
-        if (annos.containsKey(DecimalMax.class.getName())) {
-            DecimalMax max = (DecimalMax) annos.get(DecimalMax.class.getName());
-            if (property instanceof AbstractNumericProperty) {
-                AbstractNumericProperty ap = (AbstractNumericProperty) property;
+            if (annos.containsKey(DecimalMax.class.getName())) {
+                DecimalMax max = (DecimalMax) annos.get(DecimalMax.class.getName());
                 if (max.inclusive()) {
-                    ap.setMaximum(new Double(max.value()));
+                    serializable.setMaximum(new Double(max.value()));
                 } else {
-                    ap.setExclusiveMaximum(!max.inclusive());
+                    serializable.setExclusiveMaximum(!max.inclusive());
                 }
             }
-        }
-        if (annos.containsKey(Pattern.class.getName())) {
-            Pattern pattern = (Pattern) annos.get(Pattern.class.getName());
-            if (property instanceof StringProperty) {
-                StringProperty ap = (StringProperty) property;
-                ap.setPattern(pattern.regexp());
+            if (annos.containsKey(Pattern.class.getName())) {
+                Pattern pattern = (Pattern) annos.get(Pattern.class.getName());
+                serializable.setPattern(pattern.regexp());
             }
         }
     }