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);
}
};
}
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", "...");
}
}
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;
}
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;
}
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;
}
Aggregations