From 55a1fed34d163c1bba474cd157d549178b9270e7 Mon Sep 17 00:00:00 2001 From: Michael Edgar Date: Fri, 30 Jun 2023 16:50:51 -0400 Subject: [PATCH] Expose annotated XML attributes/elements when XmlAccessorType is NONE Signed-off-by: Michael Edgar --- .../openapi/api/constants/JaxbConstants.java | 10 ++++ .../dataobject/AnnotationTargetProcessor.java | 49 ++++++------------- .../scanner/dataobject/IgnoreResolver.java | 10 ++-- .../jakarta/JaxbWithNameGreeting.java | 3 ++ .../entities/javax/JaxbWithNameGreeting.java | 3 ++ 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/io/smallrye/openapi/api/constants/JaxbConstants.java b/core/src/main/java/io/smallrye/openapi/api/constants/JaxbConstants.java index 08cad103a..ede4a3405 100644 --- a/core/src/main/java/io/smallrye/openapi/api/constants/JaxbConstants.java +++ b/core/src/main/java/io/smallrye/openapi/api/constants/JaxbConstants.java @@ -1,7 +1,11 @@ package io.smallrye.openapi.api.constants; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.jboss.jandex.DotName; @@ -37,6 +41,12 @@ public class JaxbConstants { public static final List XML_WRAPPERELEMENT = Arrays.asList( DotName.createSimple("javax.xml.bind.annotation.XmlElementWrapper"), DotName.createSimple("jakarta.xml.bind.annotation.XmlElementWrapper")); + public static final List XML_ALL_BINDINGS = Collections.unmodifiableList(Stream.of( + JaxbConstants.XML_ATTRIBUTE, + JaxbConstants.XML_ELEMENT, + JaxbConstants.XML_WRAPPERELEMENT) + .flatMap(Collection::stream) + .collect(Collectors.toList())); public static final String PROP_NAME = "name"; diff --git a/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/AnnotationTargetProcessor.java b/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/AnnotationTargetProcessor.java index 7cb4b24c2..8d2ea92eb 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/AnnotationTargetProcessor.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/AnnotationTargetProcessor.java @@ -19,7 +19,6 @@ import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.FieldInfo; -import org.jboss.jandex.MethodInfo; import org.jboss.jandex.Type; import io.smallrye.openapi.api.models.media.SchemaImpl; @@ -266,44 +265,20 @@ Schema processField() { private void processFieldAnnotations(Schema fieldSchema, TypeResolver typeResolver) { String name = typeResolver.getBeanPropertyName(); - FieldInfo field = typeResolver.getField(); - if (field != null) { - if (processXmlAttr(name, - fieldSchema, - Annotations.getAnnotation(field, XML_ATTRIBUTE), - Annotations.getAnnotation(field, XML_ELEMENT), - Annotations.getAnnotation(field, XML_WRAPPERELEMENT))) { - return; - } - } - MethodInfo readMethod = typeResolver.getReadMethod(); - if (readMethod != null) { - if (processXmlAttr(name, - fieldSchema, - Annotations.getAnnotation(readMethod, XML_ATTRIBUTE), - Annotations.getAnnotation(readMethod, XML_ELEMENT), - Annotations.getAnnotation(readMethod, XML_WRAPPERELEMENT))) { - return; - } - } - MethodInfo writeMethod = typeResolver.getWriteMethod(); - if (writeMethod != null) { - if (processXmlAttr(name, - fieldSchema, - Annotations.getAnnotation(writeMethod, XML_ATTRIBUTE), - Annotations.getAnnotation(writeMethod, XML_ELEMENT), - Annotations.getAnnotation(writeMethod, XML_WRAPPERELEMENT))) { - return; + + for (AnnotationTarget target : Arrays.asList(typeResolver.getField(), typeResolver.getReadMethod(), + typeResolver.getWriteMethod())) { + if (target != null && processXmlAttr(name, fieldSchema, target)) { + break; } } } - private boolean processXmlAttr( - String name, - Schema fieldSchema, - AnnotationInstance xmlAttr, - AnnotationInstance xmlElement, - AnnotationInstance xmlWrapper) { + private boolean processXmlAttr(String name, Schema fieldSchema, AnnotationTarget target) { + AnnotationInstance xmlAttr = Annotations.getAnnotation(target, XML_ATTRIBUTE); + AnnotationInstance xmlElement = Annotations.getAnnotation(target, XML_ELEMENT); + AnnotationInstance xmlWrapper = Annotations.getAnnotation(target, XML_WRAPPERELEMENT); + if (xmlAttr == null && xmlWrapper == null && xmlElement == null) { return false; } @@ -313,6 +288,7 @@ private boolean processXmlAttr( fieldSchema.getXml().attribute(true); setXmlName(fieldSchema, name, xmlAttr); } + if (xmlWrapper != null) { setXmlIfEmpty(fieldSchema); fieldSchema.getXml().wrapped(true); @@ -322,9 +298,11 @@ private boolean processXmlAttr( return true; } } + if (xmlElement != null) { setXmlName(fieldSchema, name, xmlElement); } + return true; } @@ -332,6 +310,7 @@ private void setXmlIfEmpty(Schema schema) { if (schema.getXml() != null) { return; } + schema.setXml(new XMLImpl()); } diff --git a/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/IgnoreResolver.java b/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/IgnoreResolver.java index 2786a764b..fd94698fc 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/IgnoreResolver.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/IgnoreResolver.java @@ -75,10 +75,6 @@ public Visibility getDescendantVisibility(String propertyName, List d return Visibility.UNSET; } - public ClassInfo getClassInfoFromIndex(Type type) { - return this.index.getClass(type); - } - /** * Handler for OAS hidden @{@link Schema} */ @@ -367,6 +363,8 @@ public Visibility shouldIgnore(AnnotationTarget target, AnnotationTarget referen if (hasXmlTransient(declaringClass)) { result = Visibility.IGNORED; + } else if (isXmlExposed(target)) { + result = Visibility.EXPOSED; } else { result = getXmlVisibility(declaringClass, accessTypeRequired, flags); } @@ -378,6 +376,10 @@ boolean hasXmlTransient(AnnotationTarget target) { return Annotations.hasAnnotation(target, JaxbConstants.XML_TRANSIENT); } + boolean isXmlExposed(AnnotationTarget target) { + return Annotations.hasAnnotation(target, JaxbConstants.XML_ALL_BINDINGS); + } + Visibility getXmlVisibility(ClassInfo declaringClass, String accessTypeRequired, int flags) { String xmlAccessType = Annotations.getAnnotationValue(declaringClass, JaxbConstants.XML_ACCESSOR_TYPE); diff --git a/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/jakarta/JaxbWithNameGreeting.java b/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/jakarta/JaxbWithNameGreeting.java index 9058a53bd..18ba4e417 100644 --- a/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/jakarta/JaxbWithNameGreeting.java +++ b/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/jakarta/JaxbWithNameGreeting.java @@ -2,12 +2,15 @@ import java.util.List; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; import jakarta.xml.bind.annotation.XmlAttribute; import jakarta.xml.bind.annotation.XmlElement; import jakarta.xml.bind.annotation.XmlElementWrapper; import jakarta.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "MyGreeting") +@XmlAccessorType(XmlAccessType.NONE) public class JaxbWithNameGreeting { @XmlAttribute diff --git a/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/javax/JaxbWithNameGreeting.java b/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/javax/JaxbWithNameGreeting.java index 2a1c58efd..d3fe62dfb 100644 --- a/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/javax/JaxbWithNameGreeting.java +++ b/extension-jaxrs/src/test/java/test/io/smallrye/openapi/runtime/scanner/entities/javax/JaxbWithNameGreeting.java @@ -2,12 +2,15 @@ import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "MyGreeting") +@XmlAccessorType(XmlAccessType.NONE) public class JaxbWithNameGreeting { @XmlAttribute