Search in sources :

Example 1 with LocatorResource

use of jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource in project teamcity-rest by JetBrains.

the class LocatorAwareReader method populateLocatorDefinition.

private void populateLocatorDefinition(Swagger swagger, Class<?> cls) {
    LocatorResource locatorAnnotation = cls.getAnnotation(LocatorResource.class);
    ModelImpl definition = new ModelImpl();
    if (!swagger.getDefinitions().containsKey(locatorAnnotation.value())) {
        // case if definition is already present because of other swagger annotations present on class
        definition.setType(ModelImpl.OBJECT);
        definition.setName(locatorAnnotation.value());
        definition.setVendorExtension(ExtensionType.X_BASE_TYPE, ObjectType.LOCATOR);
        definition.setVendorExtension(ExtensionType.X_IS_LOCATOR, true);
        definition.setVendorExtension(ExtensionType.X_SUBPACKAGE, "locator");
    } else {
        definition = (ModelImpl) swagger.getDefinitions().get(locatorAnnotation.value());
    }
    // as annotation should be compile-time constant, we keep dimension names in the annotation and resolve them in runtime
    ArrayList<LocatorDimension> dimensions = new ArrayList<LocatorDimension>();
    for (String extraDimensionName : locatorAnnotation.extraDimensions()) {
        if (CommonLocatorDimensionsList.dimensionHashMap.containsKey(extraDimensionName)) {
            dimensions.add(CommonLocatorDimensionsList.dimensionHashMap.get(extraDimensionName));
        } else {
            LOGGER.warn(String.format("Common locator dimension %s was not found.", extraDimensionName));
        }
    }
    // iterate through class fields to see if any have LocatorDimension annotation
    for (Field field : cls.getDeclaredFields()) {
        // iterate through the class fields
        if (field.isAnnotationPresent(LocatorDimension.class)) {
            LocatorDimension dimension = field.getAnnotation(LocatorDimension.class);
            dimensions.add(dimension);
        }
    }
    Collections.sort(dimensions, new Comparator<LocatorDimension>() {

        @Override
        public int compare(LocatorDimension o1, LocatorDimension o2) {
            return o1.value().compareTo(o2.value());
        }
    });
    for (LocatorDimension dimension : dimensions) {
        AbstractProperty property = resolveLocatorDimensionProperty(dimension);
        definition.addProperty(dimension.value(), property);
    }
    definition.setVendorExtension(ExtensionType.X_BASE_ENTITY, locatorAnnotation.baseEntity());
    definition.setDescription(String.format("Represents a locator string for filtering %s entities.", locatorAnnotation.baseEntity()));
    if (locatorAnnotation.examples().length != 0) {
        definition.setVendorExtension(ExtensionType.X_MODEL_EXAMPLES, locatorAnnotation.examples());
    }
    swagger.addDefinition(locatorAnnotation.value(), definition);
}
Also used : Field(java.lang.reflect.Field) LocatorResource(jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource) LocatorDimension(jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)

Aggregations

Field (java.lang.reflect.Field)1 LocatorDimension (jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)1 LocatorResource (jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource)1