Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose annotated XML attributes/elements when XmlAccessorType is NONE #1505

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -37,6 +41,12 @@ public class JaxbConstants {
public static final List<DotName> XML_WRAPPERELEMENT = Arrays.asList(
DotName.createSimple("javax.xml.bind.annotation.XmlElementWrapper"),
DotName.createSimple("jakarta.xml.bind.annotation.XmlElementWrapper"));
public static final List<DotName> 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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -313,6 +288,7 @@ private boolean processXmlAttr(
fieldSchema.getXml().attribute(true);
setXmlName(fieldSchema, name, xmlAttr);
}

if (xmlWrapper != null) {
setXmlIfEmpty(fieldSchema);
fieldSchema.getXml().wrapped(true);
Expand All @@ -322,16 +298,19 @@ private boolean processXmlAttr(
return true;
}
}

if (xmlElement != null) {
setXmlName(fieldSchema, name, xmlElement);
}

return true;
}

private void setXmlIfEmpty(Schema schema) {
if (schema.getXml() != null) {
return;
}

schema.setXml(new XMLImpl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ public Visibility getDescendantVisibility(String propertyName, List<ClassInfo> d
return Visibility.UNSET;
}

public ClassInfo getClassInfoFromIndex(Type type) {
return this.index.getClass(type);
}

/**
* Handler for OAS hidden @{@link Schema}
*/
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down