protected void addOperationToSwagger() { if (StringUtils.isEmpty(httpMethod)) { return; } Path pathObj = swagger.getPath(path); if (pathObj == null) { pathObj = new Path(); swagger.path(path, pathObj); } HttpMethod hm = HttpMethod.valueOf(httpMethod.toUpperCase(Locale.US)); if (pathObj.getOperationMap().get(hm) != null) { throw new Error(String.format("Only allowed one default path. %s:%s", swaggerGenerator.getCls().getName(), providerMethod.getName())); } pathObj.set(httpMethod, operation); } }
protected void convertResponses() { if (swagger.getPaths() == null) { return; } for (Path path : swagger.getPaths().values()) { for (Operation operation : path.getOperations()) { for (Response response : operation.getResponses().values()) { convert(response.getSchema()); Map<String, Property> headers = response.getHeaders(); if (headers == null) { continue; } for (Property header : headers.values()) { convert(header); } } } } }
Map<HttpMethod, Operation> result = new LinkedHashMap<>(); if (path.getGet() != null) { result.put(HttpMethod.GET, path.getGet()); if (path.getPut() != null) { result.put(HttpMethod.PUT, path.getPut()); if (path.getPost() != null) { result.put(HttpMethod.POST, path.getPost()); if (path.getDelete() != null) { result.put(HttpMethod.DELETE, path.getDelete()); if (path.getPatch() != null) { result.put(HttpMethod.PATCH, path.getPatch()); if (path.getHead() != null) { result.put(HttpMethod.HEAD, path.getHead()); if (path.getOptions() != null) { result.put(HttpMethod.OPTIONS, path.getOptions());
Optional.ofNullable(swagger.getTag(value)) .orElseGet(() -> { Tag tag = new Tag().name(value); swagger.addTag(tag); return tag; }); Optional.ofNullable(swagger.getPath(pattern)) .orElseGet(() -> { Path path = new Path(); swagger.path(pattern, path); return path; Operation op = new Operation(); tags.forEach(it -> op.addTag(it.getName())); op.operationId(operationId(route, tags.get(0), opIds)); path.set(method(route), op);
for (final Map.Entry<String, Path> pathEntry : notNull(swaggerModelInfo.getModel().getPaths()).entrySet()) { for (final Map.Entry<HttpMethod, Operation> operationEntry : notNull(pathEntry.getValue().getOperationMap()).entrySet()) { for (final Parameter parameter : notNull(operationEntry.getValue().getParameters())) { if (!(parameter instanceof BodyParameter)) { continue; for (final Map.Entry<String, Response> responseEntry : notNull(operationEntry.getValue().getResponses()).entrySet()) { if (!responseEntry.getKey().startsWith("2")) { continue; // check only correct responses if (responseEntry.getValue().getSchema() == null) { final String message = "Operation " + operationEntry.getKey() + " " + pathEntry.getKey() + " does not provide a response schema for code " + responseEntry.getKey();
MessageContext ctx = createMessageContext(); String currentBasePath = StringUtils.substringBeforeLast(ctx.getHttpServletRequest().getRequestURI(), "/"); data.setBasePath(currentBasePath); if (data.getHost() == null) { data.setHost(beanConfig.getHost()); if (data.getInfo() == null) { for (Map.Entry<HttpMethod, Operation> subentry : entry.getValue().getOperationMap().entrySet()) { if (replaceTags && tag != null) { subentry.getValue().setTags(Collections.singletonList(tag.getName())); OperationResourceInfo ori = methods.get(key); subentry.getValue().setSummary(javadocProvider.getMethodDoc(ori)); for (int i = 0; i < subentry.getValue().getParameters().size(); i++) { subentry.getValue().getParameters().get(i). setDescription(javadocProvider.getMethodParameterDoc(ori, i)); subentry.getValue().getResponses().entrySet().iterator().next().getValue(). setDescription(javadocProvider.getMethodResponseDoc(ori));
protected void convertToInterface() { if (interfaceCls != null) { return; } ClassConfig classConfig = new ClassConfig(); classConfig.setClassName(interfaceName); classConfig.setIntf(true); if (swagger.getPaths() != null) { for (Path path : swagger.getPaths().values()) { for (Operation operation : path.getOperations()) { Response result = operation.getResponses().get(SwaggerConst.SUCCESS_KEY); JavaType resultJavaType = swaggerObjectMap.get(result.getSchema()); MethodConfig methodConfig = new MethodConfig(); methodConfig.setName(operation.getOperationId()); methodConfig.setResult(resultJavaType); for (Parameter parameter : operation.getParameters()) { String paramName = parameter.getName(); paramName = ClassUtils.correctMethodParameterName(paramName); JavaType paramJavaType = ConverterMgr.findJavaType(this, parameter); methodConfig.addParameter(paramName, paramJavaType); } classConfig.addMethod(methodConfig); } } } interfaceCls = JavassistUtils.createClass(classLoader, classConfig); }
fullPath += paths.get(0); Path path = swagger.getPath(fullPath); if (path == null) { path = new Path(); Operation op = new Operation(); op.addScheme(Scheme.HTTP); op.addScheme(Scheme.HTTPS); op.setDescription(fn.getDescription()); if (consumes != null) { op.defaultResponse(new Response()); path.set(method.toLowerCase(), op); logger.debug("afterScan; added op: {} for method {}", op, method); swagger.path(fullPath, path); logger.debug("afterScan; set path: {}", path.getOperations()); if (consumes != null) { for (String consume: consumes) { swagger.addConsumes(consume);
@Override public void afterScan(final io.swagger.jaxrs.Reader reader, final Swagger swagger) { for (final String pathName : swagger.getPaths().keySet()) { final Path path = swagger.getPaths().get(pathName); decorateOperation(path.getGet(), pathName, "GET"); decorateOperation(path.getPost(), pathName, "POST"); decorateOperation(path.getPut(), pathName, "PUT"); decorateOperation(path.getDelete(), pathName, "DELETE"); decorateOperation(path.getOptions(), pathName, "OPTIONS"); } for (final Model m : swagger.getDefinitions().values()) { if (m.getProperties() != null) { for (final Property p : m.getProperties().values()) { p.setReadOnly(false); } } } }
@Override public String generateSwaggerFromResources(CompositeAPI.Builder api) { Swagger swagger = new Swagger(); Info info = new Info(); info.setTitle(api.getName()); swagger.setInfo(info); Map<String, Path> stringPathMap = new HashMap(); for (UriTemplate uriTemplate : api.getUriTemplates().values()) { parameterList.add(getDefaultBodyParameter()); Operation operation = new Operation(); operation.setParameters(parameterList); operation.setOperationId(uriTemplate.getTemplateId()); operation.addResponse("200", getDefaultResponse()); if (stringPathMap.containsKey(uriTemplateString)) { Path path = stringPathMap.get(uriTemplateString); path.set(uriTemplate.getHttpVerb().toLowerCase(), operation); } else { Path path = new Path(); path.set(uriTemplate.getHttpVerb().toLowerCase(), operation); stringPathMap.put(uriTemplateString, path); swagger.setPaths(stringPathMap); swagger.setPaths(stringPathMap); return Json.pretty(swagger);
String securityName = getOauthSecurityName(swagger); if (!StringUtils.isEmpty(securityName)) { List<SecurityRequirement> securityRequirements = swagger.getSecurity(); if (securityRequirements != null) { for (SecurityRequirement securityRequirement : securityRequirements) { SecurityRequirement securityRequirement = new SecurityRequirement(); securityRequirement.setRequirements(securityName, api.getScopes()); swagger.addSecurity(securityRequirement); swagger.getPaths().entrySet().removeIf(entry -> isPathNotExist(uriTemplateMap, securityName, entry)); uriTemplateMap.forEach((k, v) -> { Path path = swagger.getPath(v.getUriTemplate()); if (path == null) { path = new Path(); swagger.path(v.getUriTemplate(), path); Map<HttpMethod, Operation> operationMap = path.getOperationMap(); Operation operation = operationMap.get(getHttpMethodForVerb(v.getHttpVerb().toUpperCase ())); operationTocCreate.setParameters(parameterList); path.set(v.getHttpVerb().toLowerCase(), operationTocCreate);
Map<String, Path> paths = swagger.getPaths(); if (paths == null) paths = Collections.emptyMap(); for (String path : paths.keySet()) { Map<HttpMethod, Operation> operations = paths.get(path).getOperationMap(); for (HttpMethod method : operations.keySet()) { Operation operation = operations.get(method); if (isEmpty(operation.getConsumes())) operation.setConsumes(swagger.getConsumes()); if (isEmpty(operation.getProduces())) operation.setProduces(swagger.getProduces()); Map<String, Response> responses = operation.getResponses(); Response response = responses != null ? responses.get("200") : null; DataProvider dataProvider = response != null && response.getSchema() != null ? DataProviders.getInstance().collect(swagger, response.getSchema(), true) : new ConstDataProvider(null); dataProvider.setRequired(true);
public void resolvePath(Path path){ for(Operation op : path.getOperations()) { // inputs for(Parameter parameter : op.getParameters()) { if(parameter instanceof BodyParameter) { BodyParameter body = (BodyParameter) parameter; Model schema = body.getSchema(); Model resolved = resolveModel(schema); body.setSchema(resolved); } } // responses if(op.getResponses() != null) { for(String code : op.getResponses().keySet()) { Response response = op.getResponses().get(code); if (response.getResponseSchema() != null) { Model resolved = resolveModel(response.getResponseSchema()); response.setResponseSchema(resolved); } } } } }
public static OperationDescription operationDescriptionOf(final Swagger swagger, final Operation operation) { final Entry<String, Path> pathEntry = swagger.getPaths().entrySet().stream() .filter(e -> e.getValue().getOperations().contains(operation)).findFirst().get(); final String path = pathEntry.getKey(); final Entry<HttpMethod, Operation> operationEntry = pathEntry.getValue().getOperationMap().entrySet().stream() .filter(e -> e.getValue().equals(operation)).findFirst().get(); final HttpMethod method = operationEntry.getKey(); final String specifiedSummary = trimToNull(operation.getSummary()); final String specifiedDescription = trimToNull(operation.getDescription()); final String name = ofNullable(specifiedSummary).orElseGet(() -> method + " " + path); final String description = ofNullable(specifiedDescription).orElseGet(() -> "Send " + method + " request to " + path); return new OperationDescription(name, description); }
@Test public void shouldMakeNonUniqueOperationIdsUnique() { final Swagger swagger = new Swagger().path("/path", new Path().get(new Operation().operationId("foo")) .post(new Operation().operationId("foo")).put(new Operation().operationId("bar"))); final Connector generated = generator.configureConnector(SWAGGER_TEMPLATE, new Connector.Builder().id("connector1").build(), createSettingsFrom(swagger)); final List<ConnectorAction> actions = generated.getActions(); assertThat(actions).hasSize(3); assertThat(actions.get(0).getId()).hasValueSatisfying(id -> assertThat(id).endsWith("foo")); assertThat(actions.get(1).getId()).hasValueSatisfying(id -> assertThat(id).endsWith("foo1")); assertThat(actions.get(2).getId()).hasValueSatisfying(id -> assertThat(id).endsWith("bar")); }
public SwaggerHelperOperationDescriptionGenerationTest(final String operationSummary, final String operationDescription, final String expectedName, final String expectedDescription) { operation = new Operation().description(operationDescription).summary(operationSummary); swagger = new Swagger().path("/test", new Path().get(operation)); expected = new OperationDescription(expectedName, expectedDescription); }
tag.name(tagname); tags.put(tagname, tag); swagger.addTag(tag); path = new Path(); paths.put(pattern, path); Operation op = new Operation(); op.addTag(tag.getName()); route.name().ifPresent(op::operationId); route.name().ifPresent(n -> op.summary(Route.normalize(n).substring(1))); path.set(route.method().toLowerCase(), op); } else { log.debug("skipping: {}", route); swagger.paths(paths);
Method interfaceMethod = entry.getKey(); final Operation operation = new Operation(); String operationPath = null; String httpMethod = null; if (operation.getResponses() == null) { operation.defaultResponse(new Response().description("successful operation")); final String parsedPath = PathUtils.parsePath(operationPath, regexMap); Path path = swagger.getPath(parsedPath); if (path == null) { path = new Path(); swagger.path(parsedPath, path); path.set(httpMethod.toLowerCase(), operation);
continue; final Operation operation = new Operation(); if ("post".equalsIgnoreCase(httpMethod) && operation.getConsumes() == null) { operation.addConsumes("application/x-www-form-urlencoded"); if (operation.getResponses() == null) { operation.defaultResponse(new Response().description("successful operation")); final String parsedPath = PathUtils.parsePath(operationPath, regexMap); Path path = swagger.getPath(parsedPath); if (path == null) { path = new SwaggerPath(); swagger.path(parsedPath, path); path.set(httpMethod.toLowerCase(), operation);