Search in sources :

Example 1 with DocumentationExample

use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.

the class MethodExampleImpl method getRequestHeaders.

@Override
public String getRequestHeaders() {
    String contextPath = this.resourceMethod.getContext().getRelativeContextPath();
    if (!StringUtils.isEmpty(contextPath)) {
        contextPath = "/" + contextPath;
    }
    String fullpath = contextPath + this.resourceMethod.getFullpath();
    JavaDoc.JavaDocTagList pathExample = this.resourceMethod.getJavaDoc().get("pathExample");
    if (pathExample != null && !pathExample.isEmpty()) {
        fullpath = pathExample.get(0);
    }
    StringBuilder builder = new StringBuilder(this.httpMethod).append(' ').append(fullpath).append("\n");
    if (this.requestDescriptor != null) {
        builder.append("Content-Type: ").append(this.requestDescriptor.getMediaType()).append("\n");
    }
    if (this.responseDescriptor != null) {
        builder.append("Accept: ").append(this.responseDescriptor.getMediaType()).append("\n");
    }
    Set<ResourceParameter> resourceParameters = this.resourceMethod.getResourceParameters();
    for (ResourceParameter resourceParameter : resourceParameters) {
        if ("header".equalsIgnoreCase(resourceParameter.getTypeName())) {
            if ("content-type".equalsIgnoreCase(resourceParameter.getParameterName()) && this.requestDescriptor != null) {
                continue;
            }
            if ("accept".equalsIgnoreCase(resourceParameter.getParameterName()) && this.responseDescriptor != null) {
                continue;
            }
            String exampleValue = resourceParameter.getDefaultValue() != null ? resourceParameter.getDefaultValue() : "...";
            DocumentationExample documentationExample = resourceParameter.getAnnotation(DocumentationExample.class);
            if (documentationExample != null) {
                if (documentationExample.exclude()) {
                    continue;
                }
                exampleValue = documentationExample.value();
            }
            builder.append(resourceParameter.getParameterName()).append(": ").append(exampleValue).append('\n');
        }
    }
    return builder.toString();
}
Also used : JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample)

Example 2 with DocumentationExample

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.resourceMethod, this.resourceMethod.getContext().getContext());
    if (example == null) {
        example = descriptor.getExample();
        JavaDoc.JavaDocTagList tags = this.resourceMethod.getJavaDoc().get("documentationType");
        if (tags != null && tags.size() > 0) {
            String tag = tags.get(0).trim();
            if (!tag.isEmpty()) {
                TypeElement typeElement = this.resourceMethod.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.resourceMethod.getContext().getContext().getLogger().warn("Invalid documentation type %s.", tag);
                }
            }
        }
        DocumentationExample documentationExample = this.resourceMethod.getAnnotation(DocumentationExample.class);
        if (documentationExample != null) {
            TypeMirror typeHint = TypeHintUtils.getTypeHint(documentationExample.type(), this.resourceMethod.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;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample) DeclaredType(javax.lang.model.type.DeclaredType)

Example 3 with DocumentationExample

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.putAll((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.put(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.put(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(), "...");
                            }
                        }
                        node.put(member.getName(), exampleNode);
                        exampleNode.add(itemNode);
                    }
                }
            } 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.put(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.put(member.getName(), exampleNode);
                }
            }
        } else {
            JsonType jsonType = exampleType == null ? member.getJsonType() : exampleType;
            node.put(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", "...");
    }
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) TypeElement(javax.lang.model.element.TypeElement) JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) JsonNode(org.codehaus.jackson.JsonNode) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayNode(org.codehaus.jackson.node.ArrayNode)

Example 4 with DocumentationExample

use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.

the class MethodExampleImpl method getRequestHeaders.

@Override
public String getRequestHeaders() {
    String contextPath = this.resourceMethod.getContext().getRelativeContextPath();
    if (!StringUtils.isEmpty(contextPath)) {
        contextPath = "/" + contextPath;
    }
    String fullpath = contextPath + this.resourceMethod.getFullpath();
    JavaDoc.JavaDocTagList pathExample = this.resourceMethod.getJavaDoc().get("pathExample");
    if (pathExample != null && !pathExample.isEmpty()) {
        fullpath = pathExample.get(0);
    }
    StringBuilder builder = new StringBuilder(this.httpMethod).append(' ').append(fullpath).append("\n");
    if (this.requestDescriptor != null) {
        builder.append("Content-Type: ").append(this.requestDescriptor.getMediaType()).append("\n");
    }
    if (this.responseDescriptor != null) {
        builder.append("Accept: ").append(this.responseDescriptor.getMediaType()).append("\n");
    }
    Set<RequestParameter> resourceParameters = this.resourceMethod.getRequestParameters();
    for (RequestParameter resourceParameter : resourceParameters) {
        if ("header".equalsIgnoreCase(resourceParameter.getTypeName())) {
            if ("content-type".equalsIgnoreCase(resourceParameter.getParameterName()) && this.requestDescriptor != null) {
                continue;
            }
            if ("accept".equalsIgnoreCase(resourceParameter.getParameterName()) && this.responseDescriptor != null) {
                continue;
            }
            String exampleValue = resourceParameter.getDefaultValue() != null ? resourceParameter.getDefaultValue() : "...";
            DocumentationExample documentationExample = resourceParameter.getAnnotation(DocumentationExample.class);
            if (documentationExample != null) {
                if (documentationExample.exclude()) {
                    continue;
                }
                exampleValue = documentationExample.value();
            }
            builder.append(resourceParameter.getParameterName()).append(": ").append(exampleValue).append('\n');
        }
    }
    return builder.toString();
}
Also used : JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample)

Example 5 with DocumentationExample

use of com.webcohesion.enunciate.metadata.DocumentationExample in project enunciate by stoicflame.

the class JsonExampleForMethod method findSpecifiedExample.

private String findSpecifiedExample(Property property) {
    String example = null;
    JavaDoc.JavaDocTagList tags = property.getJavaDoc().get("documentationExample");
    if (tags != null && tags.size() > 0) {
        String tag = tags.get(0).trim();
        example = tag.isEmpty() ? null : tag;
    }
    DocumentationExample documentationExample = property.getAnnotation(DocumentationExample.class);
    if (documentationExample != null) {
        if (documentationExample.exclude()) {
            return null;
        }
        example = documentationExample.value();
        example = "##default".equals(example) ? null : example;
    }
    if (example != null && (property.getDataType() == null || property.getDataType().getBaseType() == BaseType.string)) {
        example = "\"" + new String(encoder.quoteAsString(example)) + "\"";
    }
    return example;
}
Also used : JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample)

Aggregations

JavaDoc (com.webcohesion.enunciate.javac.javadoc.JavaDoc)10 DocumentationExample (com.webcohesion.enunciate.metadata.DocumentationExample)10 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)5 TypeElement (javax.lang.model.element.TypeElement)5 TypeMirror (javax.lang.model.type.TypeMirror)5 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)3 EnunciateException (com.webcohesion.enunciate.EnunciateException)2 IOException (java.io.IOException)2 Element (javax.lang.model.element.Element)2 DeclaredType (javax.lang.model.type.DeclaredType)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 DecoratedElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedElement)1 Attribute (com.webcohesion.enunciate.modules.jaxb.model.Attribute)1 ComplexTypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.ComplexTypeDefinition)1 XmlClassType (com.webcohesion.enunciate.modules.jaxb.model.types.XmlClassType)1 XmlType (com.webcohesion.enunciate.modules.jaxb.model.types.XmlType)1 JsonNode (org.codehaus.jackson.JsonNode)1