use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.
the class ComplexTypeExampleImpl method build.
private String build(Element rootElement, ComplexTypeDefinition type, final Document document, Context context) {
if (context.stack.size() > 2) {
// don't go deeper than 2 for fear of the OOM (see https://github.com/stoicflame/enunciate/issues/139).
return rootElement.getNamespaceURI();
}
if (context.stack.contains(type.getQualifiedName().toString())) {
return rootElement.getNamespaceURI();
}
String defaultNamespace = rootElement.getNamespaceURI();
context.stack.push(type.getQualifiedName().toString());
try {
FacetFilter facetFilter = registrationContext.getFacetFilter();
for (Attribute attribute : type.getAttributes()) {
if (ElementUtils.findDeprecationMessage(attribute, null) != null) {
continue;
}
if (!facetFilter.accept(attribute)) {
continue;
}
String example = "...";
JavaDoc.JavaDocTagList tags = getDocumentationExampleTags(attribute);
if (tags != null && tags.size() > 0) {
String tag = tags.get(0).trim();
example = tag.isEmpty() ? null : tag;
}
DocumentationExample documentationExample = getDocumentationExample(attribute);
if (documentationExample != null) {
if (documentationExample.exclude()) {
continue;
} else if (context.currentIndex == 1 && !"##default".equals(documentationExample.value2())) {
example = documentationExample.value2();
} else if (!"##default".equals(documentationExample.value())) {
example = documentationExample.value();
}
}
String configuredExample = getConfiguredExample(attribute);
if (configuredExample != null) {
example = configuredExample;
}
rootElement.setAttributeNS(attribute.getNamespace(), attribute.getName(), example);
if (attribute.getNamespace() == null) {
defaultNamespace = null;
}
}
if (type.getValue() != null) {
String example = "...";
JavaDoc.JavaDocTagList tags = getDocumentationExampleTags(type.getValue());
if (tags != null && tags.size() > 0) {
String tag = tags.get(0).trim();
example = tag.isEmpty() ? null : tag;
}
DocumentationExample documentationExample = getDocumentationExample(type.getValue());
if (documentationExample != null) {
if (!"##default".equals(documentationExample.value())) {
example = documentationExample.value();
}
}
String configuredExample = getConfiguredExample(type.getValue());
if (configuredExample != null) {
example = configuredExample;
}
rootElement.setTextContent(example);
} else {
for (com.webcohesion.enunciate.modules.jaxb.model.Element element : type.getElements()) {
if (ElementUtils.findDeprecationMessage(element, null) != null) {
continue;
}
if (!facetFilter.accept(element)) {
continue;
}
Element currentElement = rootElement;
if (element.isWrapped()) {
Element wrapper = document.createElementNS(element.getWrapperNamespace(), element.getWrapperName());
rootElement.appendChild(wrapper);
currentElement = wrapper;
if (element.getWrapperNamespace() == null) {
defaultNamespace = null;
}
}
for (com.webcohesion.enunciate.modules.jaxb.model.Element choice : element.getChoices()) {
Element childElement = document.createElementNS(choice.getNamespace(), choice.getName());
if (choice.getNamespace() == null) {
defaultNamespace = null;
}
XmlType baseType = choice.getXmlType();
JavaDoc.JavaDocTagList tags = choice.getJavaDoc().get("documentationType");
if (tags != null && tags.size() > 0) {
String tag = tags.get(0).trim();
if (!tag.isEmpty()) {
TypeElement typeElement = type.getContext().getContext().getProcessingEnvironment().getElementUtils().getTypeElement(tag);
if (typeElement != null) {
baseType = XmlTypeFactory.getXmlType(typeElement.asType(), type.getContext());
} else {
type.getContext().getContext().getLogger().warn("Invalid documentation type %s.", tag);
}
}
}
DocumentationExample documentationExample = getDocumentationExample(choice);
if (documentationExample != null) {
TypeMirror typeHint = TypeHintUtils.getTypeHint(documentationExample.type(), type.getContext().getContext().getProcessingEnvironment(), null);
if (typeHint != null) {
baseType = XmlTypeFactory.getXmlType(typeHint, type.getContext());
}
}
if (baseType instanceof XmlClassType && ((XmlClassType) baseType).getTypeDefinition() instanceof ComplexTypeDefinition) {
String defaultChildNs = build(childElement, (ComplexTypeDefinition) ((XmlClassType) baseType).getTypeDefinition(), document, context);
if (defaultChildNs == null) {
defaultNamespace = null;
}
} else {
String example = "...";
tags = getDocumentationExampleTags(choice);
if (tags != null && tags.size() > 0) {
String tag = tags.get(0).trim();
example = tag.isEmpty() ? null : tag;
}
if (documentationExample != null) {
if (documentationExample.exclude()) {
continue;
} else if (context.currentIndex == 1 && !"##default".equals(documentationExample.value2())) {
example = documentationExample.value2();
} else if (!"##default".equals(documentationExample.value())) {
example = documentationExample.value();
}
}
String configuredExample = getConfiguredExample(choice);
if (configuredExample != null) {
example = configuredExample;
}
childElement.setTextContent(example);
}
currentElement.appendChild(childElement);
}
}
}
XmlType supertype = type.getBaseType();
if (supertype instanceof XmlClassType && ((XmlClassType) supertype).getTypeDefinition() instanceof ComplexTypeDefinition) {
String defaultSuperNs = build(rootElement, (ComplexTypeDefinition) ((XmlClassType) supertype).getTypeDefinition(), document, context);
if (defaultSuperNs == null) {
defaultNamespace = null;
}
}
if (type.getAnyElement() != null && ElementUtils.findDeprecationMessage(type.getAnyElement(), null) == null) {
Element extension1 = document.createElementNS(defaultNamespace, "extension1");
extension1.setTextContent("...");
rootElement.appendChild(extension1);
Element extension2 = document.createElementNS(defaultNamespace, "extension2");
extension2.setTextContent("...");
rootElement.appendChild(extension2);
}
} finally {
context.stack.pop();
}
return defaultNamespace;
}
use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.
the class DataTypeExampleImpl method build.
private void build(ObjectNode node, ObjectTypeDefinition type, @Nonnull ObjectTypeDefinition sourceType, Context context) {
if (context.stack.size() > 2) {
// don't go deeper than 2 for fear of the OOM (see https://github.com/stoicflame/enunciate/issues/139).
return;
}
if (type.getTypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
if (type.getTypeIdProperty() != null) {
node.put(type.getTypeIdProperty(), sourceType.getTypeIdValue());
}
}
JsonNode override = findExampleOverride(type, type.getContext().getContext().getLogger());
if (override != null) {
if (override instanceof ObjectNode) {
node.setAll((ObjectNode) override);
return;
} else {
type.getContext().getContext().getLogger().warn("JSON example override of %s can't be used because it's not a JSON object.", type.getQualifiedName());
}
}
FacetFilter facetFilter = this.registrationContext.getFacetFilter();
for (Member member : type.getMembers()) {
if (node.has(member.getName())) {
continue;
}
if (!facetFilter.accept(member)) {
continue;
}
if (ElementUtils.findDeprecationMessage(member, null) != null) {
continue;
}
JsonNode memberOverride = findExampleOverride(member, type.getContext().getContext().getLogger());
if (memberOverride != null) {
node.set(member.getName(), memberOverride);
continue;
}
String example = null;
String example2 = null;
JsonType exampleType = null;
JavaDoc.JavaDocTagList tags = getDocumentationExampleTags(member);
if (tags != null && tags.size() > 0) {
String tag = tags.get(0).trim();
example = tag.isEmpty() ? null : tag;
example2 = example;
if (tags.size() > 1) {
tag = tags.get(1).trim();
example2 = tag.isEmpty() ? null : tag;
}
}
tags = member.getJavaDoc().get("documentationType");
if (tags != null && tags.size() > 0) {
String tag = tags.get(0).trim();
if (!tag.isEmpty()) {
TypeElement typeElement = type.getContext().getContext().getProcessingEnvironment().getElementUtils().getTypeElement(tag);
if (typeElement != null) {
exampleType = JsonTypeFactory.getJsonType(typeElement.asType(), type.getContext());
} else {
type.getContext().getContext().getLogger().warn("Invalid documentation type %s.", tag);
}
}
}
DocumentationExample documentationExample = getDocumentationExample(member);
if (documentationExample != null) {
if (documentationExample.exclude()) {
continue;
}
example = documentationExample.value();
example = "##default".equals(example) ? null : example;
example2 = documentationExample.value2();
example2 = "##default".equals(example2) ? null : example2;
TypeMirror typeHint = TypeHintUtils.getTypeHint(documentationExample.type(), type.getContext().getContext().getProcessingEnvironment(), null);
if (typeHint != null) {
exampleType = JsonTypeFactory.getJsonType(typeHint, type.getContext());
}
}
String specifiedTypeInfoValue = findSpecifiedTypeInfoValue(member, type.getQualifiedName().toString(), type);
if (specifiedTypeInfoValue != null) {
example = specifiedTypeInfoValue;
example2 = specifiedTypeInfoValue;
}
String configuredExample = getConfiguredExample(member);
if (configuredExample != null) {
example = configuredExample;
example2 = configuredExample;
}
if (context.currentIndex % 2 > 0) {
// if our index is odd, switch example 1 and example 2.
String placeholder = example2;
example2 = example;
example = placeholder;
}
if (member.getChoices().size() > 1) {
if (member.isCollectionType()) {
final ArrayNode exampleNode = JsonNodeFactory.instance.arrayNode();
for (Member choice : member.getChoices()) {
JsonType jsonType = exampleType == null ? choice.getJsonType() : exampleType;
String choiceName = choice.getName();
if ("".equals(choiceName)) {
choiceName = "...";
}
if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_ARRAY) {
ArrayNode wrapperNode = JsonNodeFactory.instance.arrayNode();
wrapperNode.add(choiceName);
wrapperNode.add(exampleNode(jsonType, example, example2, context));
exampleNode.add(wrapperNode);
} else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_OBJECT) {
ObjectNode wrapperNode = JsonNodeFactory.instance.objectNode();
wrapperNode.set(choiceName, exampleNode(jsonType, example, example2, context));
exampleNode.add(wrapperNode);
} else {
JsonNode itemNode = exampleNode(jsonType, example, example2, context);
if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
if (member.getSubtypeIdProperty() != null && itemNode instanceof ObjectNode) {
((ObjectNode) itemNode).put(member.getSubtypeIdProperty(), "...");
}
} else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {
if (member.getSubtypeIdProperty() != null) {
node.put(member.getSubtypeIdProperty(), "...");
}
}
exampleNode.add(itemNode);
}
}
node.set(member.getName(), exampleNode);
} else {
for (Member choice : member.getChoices()) {
JsonNode exampleNode;
JsonType jsonType = exampleType == null ? choice.getJsonType() : exampleType;
String choiceName = choice.getName();
if ("".equals(choiceName)) {
choiceName = "...";
}
if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_ARRAY) {
ArrayNode wrapperNode = JsonNodeFactory.instance.arrayNode();
wrapperNode.add(choiceName);
wrapperNode.add(exampleNode(jsonType, example, example2, context));
exampleNode = wrapperNode;
} else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.WRAPPER_OBJECT) {
ObjectNode wrapperNode = JsonNodeFactory.instance.objectNode();
wrapperNode.set(choiceName, exampleNode(jsonType, example, example2, context));
exampleNode = wrapperNode;
} else {
exampleNode = exampleNode(jsonType, example, example2, context);
if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
if (member.getSubtypeIdProperty() != null && exampleNode instanceof ObjectNode) {
((ObjectNode) exampleNode).put(member.getSubtypeIdProperty(), "...");
}
} else if (member.getSubtypeIdInclusion() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {
if (member.getSubtypeIdProperty() != null) {
node.put(member.getSubtypeIdProperty(), "...");
}
}
}
node.set(member.getName(), exampleNode);
}
}
} else {
JsonType jsonType = exampleType == null ? member.getJsonType() : exampleType;
node.set(member.getName(), exampleNode(jsonType, example, example2, context));
}
}
JsonType supertype = type.getSupertype();
if (supertype instanceof JsonClassType && ((JsonClassType) supertype).getTypeDefinition() instanceof ObjectTypeDefinition) {
build(node, (ObjectTypeDefinition) ((JsonClassType) supertype).getTypeDefinition(), sourceType, context);
}
if (type.getWildcardMember() != null && ElementUtils.findDeprecationMessage(type.getWildcardMember(), null) == null && !ExampleUtils.isExcluded(type.getWildcardMember())) {
node.put("extension1", "...");
node.put("extension2", "...");
}
}
use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.
the class DataTypeExampleImpl method findExampleOverride.
private JsonNode findExampleOverride(DecoratedElement el, EnunciateLogger logger) {
String overrideValue = null;
JavaDoc.JavaDocTagList overrideTags = el.getJavaDoc().get("jsonExampleOverride");
if (overrideTags != null && !overrideTags.isEmpty()) {
overrideValue = overrideTags.get(0);
}
DocumentationExample annotation = (DocumentationExample) el.getAnnotation(DocumentationExample.class);
if (annotation != null && !"##default".equals(annotation.jsonOverride())) {
overrideValue = annotation.jsonOverride();
}
if (overrideValue != null) {
try {
return MAPPER.readTree(overrideValue);
} catch (Exception e) {
logger.error("Unable to parse example override of element %s: %s", el.toString(), e.getMessage());
}
}
return null;
}
use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.
the class DataTypeExampleImpl method findExampleOverride.
private JsonNode findExampleOverride(DecoratedElement el, EnunciateLogger logger) {
String overrideValue = null;
JavaDoc.JavaDocTagList overrideTags = el.getJavaDoc().get("jsonExampleOverride");
if (overrideTags != null && !overrideTags.isEmpty()) {
overrideValue = overrideTags.get(0);
}
DocumentationExample annotation = (DocumentationExample) el.getAnnotation(DocumentationExample.class);
if (annotation != null && !"##default".equals(annotation.jsonOverride())) {
overrideValue = annotation.jsonOverride();
}
if (overrideValue != null) {
try {
return MAPPER.readTree(overrideValue);
} catch (Exception e) {
logger.error("Unable to parse example override of element %s: %s", el.toString(), e.getMessage());
}
}
return null;
}
use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.
the class ResponseEntityImpl method loadExample.
protected Example loadExample(Syntax syntax, MediaTypeDescriptor descriptor) {
Example example = ExampleUtils.loadCustomExample(syntax, "responseExample", this.requestMapping, this.requestMapping.getContext().getContext());
if (example == null) {
example = descriptor.getExample();
JavaDoc.JavaDocTagList tags = this.requestMapping.getJavaDoc().get("documentationType");
if (tags != null && tags.size() > 0) {
String tag = tags.get(0).trim();
if (!tag.isEmpty()) {
TypeElement typeElement = this.requestMapping.getContext().getContext().getProcessingEnvironment().getElementUtils().getTypeElement(tag);
if (typeElement != null) {
List<DataType> dataTypes = syntax.findDataTypes(typeElement.getQualifiedName().toString());
if (dataTypes != null && !dataTypes.isEmpty()) {
example = dataTypes.get(0).getExample();
}
} else {
this.requestMapping.getContext().getContext().getLogger().warn("Invalid documentation type %s.", tag);
}
}
}
DocumentationExample documentationExample = this.requestMapping.getAnnotation(DocumentationExample.class);
if (documentationExample != null) {
TypeMirror typeHint = TypeHintUtils.getTypeHint(documentationExample.type(), this.requestMapping.getContext().getContext().getProcessingEnvironment(), null);
if (typeHint instanceof DeclaredType) {
Element element = ((DeclaredType) typeHint).asElement();
if (element instanceof TypeElement) {
List<DataType> dataTypes = syntax.findDataTypes(((TypeElement) element).getQualifiedName().toString());
if (dataTypes != null && !dataTypes.isEmpty()) {
example = dataTypes.get(0).getExample();
}
}
}
}
}
return example;
}
Aggregations