Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.advantageous.qbit.meta.swagger;
  
 
 
 import java.util.*;
 
 
 
     public ServiceEndpointInfo serviceEndpointInfo(final ContextMeta contextMeta) {
 
         final ServiceEndpointInfoBuilder builder = new ServiceEndpointInfoBuilder();
 
         builder.getApiInfoBuilder().getContactBuilder().setEmail(contextMeta.getContactEmail());
         builder.getApiInfoBuilder().getContactBuilder().setName(contextMeta.getContactName());
         builder.getApiInfoBuilder().getContactBuilder().setUrl(contextMeta.getContactURL());
         builder.getApiInfoBuilder().getLicenseBuilder().setName(contextMeta.getLicenseName());
         builder.getApiInfoBuilder().getLicenseBuilder().setUrl(contextMeta.getLicenseURL());
 
         builder.setBasePath(contextMeta.getRootURI());
         builder.setHost(contextMeta.getHostAddress());
 
         final List<ServiceMetaservices = contextMeta.getServices();
         final Map<StringPathBuilderpathBuilderMap = new HashMap<>();
 
         buildDefinitions(services);
 
 
 
         for (ServiceMeta serviceMeta : services) {
 
             final List<ServiceMethodMetamethodMetas = serviceMeta.getMethods();
             for (ServiceMethodMeta methodMeta : methodMetas) {
                 final List<RequestMetarequestEndpoints = methodMeta.getRequestEndpoints();
                 final MethodAccess methodAccess = methodMeta.getMethodAccess();
 
 
                 for (RequestMeta requestMeta : requestEndpoints) {
                     final String requestURI = requestMeta.getRequestURI();
 
                     PathBuilder pathBuilder = pathBuilderMap.get(requestURI);
                     if (pathBuilder == null) {
                         pathBuilder = new PathBuilder();
                         pathBuilderMap.put(requestURIpathBuilder);
                     }
 
                     final List<RequestMethodrequestMethods = requestMeta.getRequestMethods();
 
 
                     for (RequestMethod requestMethod : requestMethods) {
                         final OperationBuilder operationBuilder = new OperationBuilder();
 
 
                         addParameters(operationBuilderrequestMeta.getParameters(), methodAccess,
                                 requestMeta.getRequestURI(), contextMeta.getRootURI());
                         operationBuilder.setOperationId(methodAccess.name());
 
                         if (methodMeta.hasCallBack() || methodAccess.returnType() != void.class) {
 
                             ResponseBuilder responseBuilder = new ResponseBuilder();
 
                             responseBuilder.setSchema(.getSchema(methodAccess.returnType()));
                             operationBuilder.getResponses().put(200, responseBuilder.build());
                             operationBuilder.getProduces().add("application/json");
 
                         } else {
                             ResponseBuilder responseBuilder = new ResponseBuilder();
                             SchemaBuilder schemaBuilder = new SchemaBuilder();
                             schemaBuilder.setType("string");
                             operationBuilder.getResponses().put(201, responseBuilder.build());
                         }
 
                         switch (requestMethod) {
                             case :
                                 pathBuilder.setGet(operationBuilder.build());
                                 break;
                             case :
                                 pathBuilder.setPost(operationBuilder.build());
                                 break;
                             case :
                                 pathBuilder.setPut(operationBuilder.build());
                                 break;
                             case :
                                 pathBuilder.setOptions(operationBuilder.build());
                                 break;
                             case :
                                 pathBuilder.setDelete(operationBuilder.build());
                                 break;
                             case :
                                 pathBuilder.setHead(operationBuilder.build());
                                 break;
                        }
                    }
                }
            }
        }
        final Set<Map.Entry<StringPathBuilder>> pathEntries = pathBuilderMap.entrySet();
        for (Map.Entry<StringPathBuilderentry : pathEntries) {
            builder.addPath(entry.getKey(), entry.getValue().build());
        }
        final Map<StringDefinitiondefinitionMap = .getDefinitionMap();
        definitionMap.entrySet().forEach(entry -> {
            builder.addDefinition(entry.getKey(), entry.getValue());
        });
        return builder.build();
    }
    private void addParameters(final OperationBuilder operationBuilder,
                               final List<ParameterMetaparameterMetaList,
                               final MethodAccess methodAccessfinal String requestURI,
                               final String baseURI
                               ) {
        final String fullURI = (baseURI + requestURI).replaceAll("//""/");
        final String[] uriParts = Str.split(requestURI'/');
        for (ParameterMeta parameterMeta : parameterMetaList) {
            ParameterBuilder parameterBuilder = new ParameterBuilder();
            if (parameterMeta.getParam() instanceof NamedParam) {
                parameterBuilder.setName(((NamedParamparameterMeta.getParam()).getName());
            }
            if (parameterMeta.getParam() instanceof RequestParam) {
                parameterBuilder.setIn("query");
            }
            if (parameterMeta.getParam() instanceof URINamedParam) {
                parameterBuilder.setIn("path");
            }
            if (parameterMeta.getParam() instanceof HeaderParam) {
                parameterBuilder.setIn("header");
            }
            if (parameterMeta.getParam() instanceof URIPositionalParam) {
                parameterBuilder.setIn("THIS QBIT FEATURE URI POSITIONAL PARAM IS NOT SUPPORTED BY SWAGGER");
            }
            if (parameterMeta.getParam() instanceof BodyArrayParam) {
                parameterBuilder.setIn("THIS QBIT FEATURE BodyArrayParam IS NOT SUPPORTED BY SWAGGER");
            }
            if (parameterMeta.getParam() instanceof BodyParam) {
                parameterBuilder.setIn("body");
                parameterBuilder.setName("body");

                
TODO handle generic types
                if (parameterMeta.getType() == .) {
                    parameterBuilder.setSchema(Schema.definitionRef(parameterMeta.getClassType().getSimpleName()));
                    parameterBuilder.setRequired(parameterMeta.getParam().isRequired());
                    operationBuilder.addParameter(parameterBuilder.build());
                    return;
                }
            }
            Schema schema = .getSchema(parameterMeta.getClassType());
            parameterBuilder.setType(schema.getType());
            if (schema.getType().equals("array")) {
                parameterBuilder.setItems(schema.getItems());
                parameterBuilder.setCollectionFormat("csv");
            }
            parameterBuilder.setRequired(parameterMeta.getParam().isRequired());
            operationBuilder.addParameter(parameterBuilder.build());
        }
    }
    private void buildDefinitions(final List<ServiceMetaservices) {
        services.forEach(serviceMeta -> {
            populateDefinitionMapByService(serviceMeta);
        });
    }
    private void populateDefinitionMapByService(final ServiceMeta serviceMeta) {
        serviceMeta.getMethods().forEach(serviceMethodMeta -> populateDefinitionMapByServiceMethod(serviceMethodMeta));
    }
    private void populateDefinitionMapByServiceMethod(final ServiceMethodMeta serviceMethodMeta) {
        MethodAccess methodAccess = serviceMethodMeta.getMethodAccess();
        .addClass(methodAccess.returnType());
        for (Class<?> paramType :  methodAccess.parameterTypes()) {
            .addClass(paramType);
        }
    }
New to GrepCode? Check out our FAQ X