@Override public String toString() { return getQualifier() + ":" + getType(); }
private AttributeBuilder() { result = new Attribute(); }
@Override public Map<String, ?> resolve(final Attribute attribute) { final ImmutableMap.Builder<String, Object> attributeMeta = ImmutableMap.builder(); if (attribute.getType() instanceof Enumeration) { final Enumeration enumValues = (Enumeration) attribute.getType(); attributeMeta.put("enum", SchemaJSONUtils.processEnumValues(enumValues)); } else if (attribute.getType() instanceof Dto) { final Dto attributeType = (Dto) attribute.getType(); attributeMeta.put("$ref", attributeType.getSchemaName()); } else { attributeMeta.put("type", SchemaJSONUtils.resolveJSONPrimitiveType(attribute)); } if (attribute.getDocumentation() != null) { attributeMeta.put("description", attribute.getDocumentation()); } return attributeMeta.build(); } }
public OneOfAttributeBuilder addAlternative(final Attribute newAttribute) { if (newAttribute != null) { if (result.getAlternativeByTypeQulifier(newAttribute.getType()) != null) { throw new IllegalArgumentException("Cannot add more than one oneOf alternative Attribute with the same Type."); } result.alternatives.add(newAttribute); } return this; }
public Attribute getAttribute(final String attributeQualifier) { for (final Attribute attribute : attributes) { if (attributeQualifier.equals(attribute.getQualifier())) { return attribute; } } return null; }
/** * A hook method that creates a simple {@code Attribute} for the {@link Dto}, i.e. an {@code Attribute} that is not a * {@link OneOfAttribute}. * <p/> * Called by the {@link #createAttributes(JDefinedClass, APINamespace)} method. Also called by * {@link #createOneOfAttribute(APINamespace, String, Collection)} for creating each alternative of a * {@code OneOfAttribute}. Subclasses may override it to customize the way that {@code Attributes} are created. * * @param namespace The {@code APINamespace} to use for the new {@code Attribute}. * @param field The {@code JFieldVar} to transform into the new {@code Attribute}. * @return The new {@code Attribute} to be added to the {@link Dto}. */ protected Attribute createSimpleAttribute(final APINamespace namespace, final JFieldVar field) { final Type typeModel = createType(namespace, field); final AttributeBuilder attributeBuilder = Attribute.builder() .qualifier(field.name()) .type(typeModel); final Collection<JAnnotationUse> annotations = field.annotations(); if (annotations != null) { for (final JAnnotationUse annotation : annotations) { final AnnotationBuilder annotationBuilder = processFieldAnnotation(annotation); attributeBuilder.annotation(annotationBuilder.build()); } } return attributeBuilder.build(); }
public static String resolveJSONPrimitiveType(final Attribute attr) { final Type type = attr.getType(); // TODO adjust it for all JSON types if (String.class.getSimpleName().equalsIgnoreCase(type.getQualifier())) { return type.getQualifier().toLowerCase(); } else if (Integer.class.getSimpleName().equalsIgnoreCase(type.getQualifier())) { return type.getQualifier().toLowerCase(); } else if (Object.class.getSimpleName().equalsIgnoreCase(type.getQualifier())) { return type.getQualifier().toLowerCase(); } return type.getQualifier(); }
@Override public Map<String, ?> resolve(final Dto type) { final ImmutableMap.Builder<String, Object> map = ImmutableMap.builder(); map.put("$schema", JSON_SCHEMA); map.put("type", "object"); final List<Attribute> attrs = type.getSimpleAttributes(); if (attrs != null && !attrs.isEmpty()) { final ImmutableMap.Builder<String, Object> attributeWrapper = ImmutableMap.builder(); for (final Attribute attr : type.getSimpleAttributes()) { final Map<String, ?> attrAsSchema = propertyResolver.resolve(attr); attributeWrapper.put(attr.getQualifier(), attrAsSchema); } map.put("properties", attributeWrapper.build()); } return map.build(); } }
/** * Convenience method that retrieves the alternative with a given {@code Type}. * * @see #getAlternatives() */ public Attribute getAlternativeByTypeQulifier(final Type alternativeType) { for (final Attribute attr : getAlternatives()) { if (attr.getType().getQualifier().equals(alternativeType.getQualifier())) { return attr; } } return null; }