Search in sources :

Example 26 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class SwaggerModule method getApiRegistry.

@Override
public ApiRegistry getApiRegistry() {
    return new ApiRegistry() {

        @Override
        public List<ServiceApi> getServiceApis(ApiRegistrationContext context) {
            return Collections.emptyList();
        }

        @Override
        public List<ResourceApi> getResourceApis(ApiRegistrationContext context) {
            return Collections.emptyList();
        }

        @Override
        public Set<Syntax> getSyntaxes(ApiRegistrationContext context) {
            return Collections.emptySet();
        }

        @Override
        public InterfaceDescriptionFile getSwaggerUI() {
            Set<String> facetIncludes = new TreeSet<String>(enunciate.getConfiguration().getFacetIncludes());
            facetIncludes.addAll(getFacetIncludes());
            Set<String> facetExcludes = new TreeSet<String>(enunciate.getConfiguration().getFacetExcludes());
            facetExcludes.addAll(getFacetExcludes());
            FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
            ApiRegistrationContext context = new SwaggerRegistrationContext(facetFilter);
            List<ResourceApi> resourceApis = apiRegistry.getResourceApis(context);
            if (resourceApis == null || resourceApis.isEmpty()) {
                info("No resource APIs registered: Swagger UI will not be generated.");
            }
            return new SwaggerInterfaceDescription(resourceApis, context);
        }
    };
}
Also used : ResourceApi(com.webcohesion.enunciate.api.resources.ResourceApi) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) ServiceApi(com.webcohesion.enunciate.api.services.ServiceApi) ApiRegistrationContext(com.webcohesion.enunciate.api.ApiRegistrationContext) Syntax(com.webcohesion.enunciate.api.datatype.Syntax) ApiRegistry(com.webcohesion.enunciate.api.ApiRegistry)

Example 27 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter 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", "...");
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) TypeElement(javax.lang.model.element.TypeElement) JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) JsonNode(com.fasterxml.jackson.databind.JsonNode) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 28 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class ObjectDataTypeImpl method getProperties.

@Override
public List<? extends Property> getProperties() {
    List<Member> members = this.typeDefinition.getMembers();
    ArrayList<Property> properties;
    if (this.typeDefinition.getTypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
        properties = new ArrayList<Property>(members.size() + 1);
        if (this.typeDefinition.getTypeIdProperty() != null && members.stream().noneMatch(m -> this.typeDefinition.getTypeIdProperty().equals(m.getName()))) {
            properties.add(new TypeReferencePropertyImpl(this.typeDefinition.getTypeIdProperty()));
        }
    } else {
        properties = new ArrayList<Property>(members.size());
    }
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    for (Member member : members) {
        if (!facetFilter.accept(member)) {
            continue;
        }
        if (member.getChoices().size() > 1) {
            JsonTypeInfo.As inclusion = member.getSubtypeIdInclusion();
            if (inclusion == JsonTypeInfo.As.WRAPPER_ARRAY || inclusion == JsonTypeInfo.As.WRAPPER_OBJECT) {
                for (Member choice : member.getChoices()) {
                    properties.add(new PropertyImpl(choice, registrationContext, member.isCollectionType()));
                }
            } else {
                properties.add(new PropertyImpl(member, registrationContext));
            }
        } else {
            properties.add(new PropertyImpl(member, registrationContext));
        }
    }
    return properties;
}
Also used : TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) java.util(java.util) JsonType(com.webcohesion.enunciate.modules.jackson.model.types.JsonType) TypeElement(javax.lang.model.element.TypeElement) JsonClassType(com.webcohesion.enunciate.modules.jackson.model.types.JsonClassType) ApiRegistrationContext(com.webcohesion.enunciate.api.ApiRegistrationContext) com.webcohesion.enunciate.api.datatype(com.webcohesion.enunciate.api.datatype) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) JsonTypeFactory(com.webcohesion.enunciate.modules.jackson.model.types.JsonTypeFactory) TypeMirror(javax.lang.model.type.TypeMirror) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) DeclaredType(javax.lang.model.type.DeclaredType) Member(com.webcohesion.enunciate.modules.jackson.model.Member) ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.ObjectTypeDefinition) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Member(com.webcohesion.enunciate.modules.jackson.model.Member)

Example 29 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class EnumDataTypeImpl method getValues.

@Override
public List<? extends Value> getValues() {
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    List<EnumValue> enumValues = this.typeDefinition.getEnumValues();
    ArrayList<Value> values = new ArrayList<Value>(enumValues.size());
    for (EnumValue enumValue : enumValues) {
        if (enumValue.getValue() != null) {
            if (!facetFilter.accept(enumValue)) {
                continue;
            }
            values.add(createValue(enumValue));
        }
    }
    return values;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) EnumValue(com.webcohesion.enunciate.modules.jackson1.model.EnumValue) EnumValue(com.webcohesion.enunciate.modules.jackson1.model.EnumValue) Value(com.webcohesion.enunciate.api.datatype.Value) ArrayList(java.util.ArrayList)

Example 30 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class ObjectDataTypeImpl method getProperties.

@Override
public List<? extends Property> getProperties() {
    List<Member> members = this.typeDefinition.getMembers();
    ArrayList<Property> properties;
    if (this.typeDefinition.getTypeIdInclusion() == JsonTypeInfo.As.PROPERTY) {
        properties = new ArrayList<Property>(members.size() + 1);
        if (this.typeDefinition.getTypeIdProperty() != null && members.stream().noneMatch(m -> this.typeDefinition.getTypeIdProperty().equals(m.getName()))) {
            properties.add(new TypeReferencePropertyImpl(this.typeDefinition.getTypeIdProperty()));
        }
    } else {
        properties = new ArrayList<Property>(members.size());
    }
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    for (Member member : members) {
        if (!facetFilter.accept(member)) {
            continue;
        }
        if (member.getChoices().size() > 1) {
            JsonTypeInfo.As inclusion = member.getSubtypeIdInclusion();
            if (inclusion == JsonTypeInfo.As.WRAPPER_ARRAY || inclusion == JsonTypeInfo.As.WRAPPER_OBJECT) {
                for (Member choice : member.getChoices()) {
                    properties.add(new PropertyImpl(choice, registrationContext, member.isCollectionType()));
                }
            } else {
                properties.add(new PropertyImpl(member, registrationContext));
            }
        } else {
            properties.add(new PropertyImpl(member, registrationContext));
        }
    }
    return properties;
}
Also used : JsonTypeFactory(com.webcohesion.enunciate.modules.jackson1.model.types.JsonTypeFactory) java.util(java.util) JsonType(com.webcohesion.enunciate.modules.jackson1.model.types.JsonType) TypeElement(javax.lang.model.element.TypeElement) TypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition) JsonTypeInfo(org.codehaus.jackson.annotate.JsonTypeInfo) ApiRegistrationContext(com.webcohesion.enunciate.api.ApiRegistrationContext) com.webcohesion.enunciate.api.datatype(com.webcohesion.enunciate.api.datatype) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType) ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.ObjectTypeDefinition) JsonClassType(com.webcohesion.enunciate.modules.jackson1.model.types.JsonClassType) Member(com.webcohesion.enunciate.modules.jackson1.model.Member) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) JsonTypeInfo(org.codehaus.jackson.annotate.JsonTypeInfo) Member(com.webcohesion.enunciate.modules.jackson1.model.Member)

Aggregations

FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)33 EnunciateException (com.webcohesion.enunciate.EnunciateException)11 TemplateException (freemarker.template.TemplateException)10 URL (java.net.URL)10 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)7 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)7 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)7 ResourceGroup (com.webcohesion.enunciate.api.resources.ResourceGroup)6 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 TypeElement (javax.lang.model.element.TypeElement)6 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)5 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)5 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)5 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)5 TypeMirror (javax.lang.model.type.TypeMirror)5 EnunciateJaxbContext (com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext)4