@Override
  public com.google.common.base.Optional<Model> modelFor(ModelContext modelContext) {
    ResolvedType propertiesHost = modelContext.alternateFor(modelContext.resolvedType(resolver));
    if (isContainerType(propertiesHost)
        || isMapType(propertiesHost)
        || propertiesHost.getErasedType().isEnum()
        || isBaseType(Types.typeNameFor(propertiesHost.getErasedType()))
        || modelContext.hasSeenBefore(propertiesHost)) {
      LOG.debug(
          "Skipping model of type {} as its either a container type, map, enum or base type, or its already "
              + "been handled",
          resolvedTypeSignature(propertiesHost).or("<null>"));
      return Optional.absent();
    }
    ImmutableMap<String, ModelProperty> propertiesIndex =
        uniqueIndex(properties(modelContext, propertiesHost), byPropertyName());
    LOG.debug(
        "Inferred {} properties. Properties found {}",
        propertiesIndex.size(),
        Joiner.on(", ").join(propertiesIndex.keySet()));

    SortedSet<Map.Entry<String, ModelProperty>> sortedProperties =
        new TreeSet<Map.Entry<String, ModelProperty>>(
            new Comparator<Map.Entry<String, ModelProperty>>() {
              @Override
              public int compare(
                  Map.Entry<String, ModelProperty> e1, Map.Entry<String, ModelProperty> e2) {
                if ((e1.getValue().getPosition().compareTo(e2.getValue().getPosition()) == 0)) {
                  return e1.getValue().getName().compareTo(e2.getValue().getName());
                }
                return new Integer(e1.getValue().getPosition())
                    .compareTo(e2.getValue().getPosition());
              }
            });

    sortedProperties.addAll(propertiesIndex.entrySet());

    Map<String, ModelProperty> properties = new LinkedHashMap<String, ModelProperty>();

    // Map<String, ModelProperty> properties = newTreeMap();
    // properties.putAll(propertiesIndex);
    for (Map.Entry<String, ModelProperty> value : sortedProperties) {
      properties.put(value.getKey(), value.getValue());
    }
    return Optional.of(modelBuilder(propertiesHost, properties, modelContext));
  }
  @Override
  public void execute(RequestMappingContext context) {
    HandlerMethod handlerMethod = context.getHandlerMethod();

    log.debug("Reading models for handlerMethod |{}|", handlerMethod.getMethod().getName());

    Map<String, Model> modelMap = newHashMap();
    SwaggerGlobalSettings swaggerGlobalSettings =
        (SwaggerGlobalSettings) context.get("swaggerGlobalSettings");
    HandlerMethodResolver handlerMethodResolver =
        new HandlerMethodResolver(swaggerGlobalSettings.getTypeResolver());
    ResolvedType modelType =
        ModelUtils.handlerReturnType(swaggerGlobalSettings.getTypeResolver(), handlerMethod);

    ApiOperation apiOperationAnnotation = handlerMethod.getMethodAnnotation(ApiOperation.class);
    if (null != apiOperationAnnotation && Void.class != apiOperationAnnotation.response()) {
      modelType =
          asResolved(swaggerGlobalSettings.getTypeResolver(), apiOperationAnnotation.response());
    }
    if (!swaggerGlobalSettings.getIgnorableParameterTypes().contains(modelType.getErasedType())) {
      ModelContext modelContext = ModelContext.returnValue(modelType);
      markIgnorablesAsHasSeen(swaggerGlobalSettings.getIgnorableParameterTypes(), modelContext);
      Optional<Model> model = modelProvider.modelFor(modelContext);
      if (model.isPresent() && !"void".equals(model.get().name())) {
        log.debug(
            "Swagger generated parameter model id: {}, name: {}, schema: {} models",
            model.get().id(),
            model.get().name());
        modelMap.put(model.get().id(), model.get());
      } else {
        log.debug("Swagger core did not find any models");
      }
      populateDependencies(modelContext, modelMap);
    }
    modelMap.putAll(
        readParametersApiModel(handlerMethodResolver, swaggerGlobalSettings, handlerMethod));

    log.debug(
        "Finished reading models for handlerMethod |{}|", handlerMethod.getMethod().getName());
    context.put("models", modelMap);
  }
Esempio n. 3
0
 @Override
 public Class<?> getReturnClass() {
   return type.getErasedType();
 }
Esempio n. 4
0
 public static boolean isVoid(ResolvedType returnType) {
   return Void.class.equals(returnType.getErasedType())
       || Void.TYPE.equals(returnType.getErasedType());
 }
Esempio n. 5
0
 @Override
 public boolean appliesTo(ResolvedType type) {
   return type.getErasedType() == genericType
       && !type.getTypeBindings().isEmpty()
       && boundTypeIndex <= type.getTypeBindings().size() - 1;
 }