From d1cdf8f4434aa50f5c533d97ef7e391924d4560c Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Mon, 20 Apr 2020 08:43:35 +0800 Subject: [PATCH] :white_check_mark: Adding tests. --- .../core/common/QueryStringBuilder.java | 16 ++++++------- .../java/com/kim/apidoc/core/common/URI.java | 24 +++++++++---------- .../com/kim/apidoc/core/schema/Section.java | 10 ++++++-- .../src/main/resources/templates/markdown.ftl | 6 ++--- .../com/kim/apidoc/example/common/Query.java | 2 +- ...serController.java => UserController.java} | 14 +++++------ 6 files changed, 38 insertions(+), 34 deletions(-) rename apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/{KimUserController.java => UserController.java} (74%) diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/QueryStringBuilder.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/QueryStringBuilder.java index 1bfb727..a5886cb 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/QueryStringBuilder.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/QueryStringBuilder.java @@ -9,8 +9,8 @@ public class QueryStringBuilder { private StringBuilder builder = new StringBuilder(); - public QueryStringBuilder append(Object key, Object value){ - if(builder.length()>0){ + public QueryStringBuilder append(Object key, Object value) { + if (builder.length() > 0) { builder.append("&"); } builder.append(key); @@ -19,21 +19,21 @@ public class QueryStringBuilder { return this; } - public QueryStringBuilder append(ObjectNode objectNode){ + public QueryStringBuilder append(ObjectNode objectNode) { Iterator iterator = objectNode.fieldNames(); - while (iterator.hasNext()){ + while (iterator.hasNext()) { String key = iterator.next(); JsonNode valueNode = objectNode.get(key); - String value = valueNode.isTextual()?valueNode.asText():valueNode.toString(); - append(key,value); + String value = valueNode.isTextual() ? valueNode.asText() : valueNode.toString(); + append(key, value); } return this; } - public String toString(){ + @Override + public String toString() { return builder.toString(); } - } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/URI.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/URI.java index c053981..3ae00f5 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/URI.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/URI.java @@ -24,34 +24,34 @@ public class URI { this.text = text; } - public URI add(String text){ + public URI add(String text) { return add(new URI(text)); } - public URI add(URI uri){ - if(next!=null){ + public URI add(URI uri) { + if (next != null) { next.add(uri); - }else{ + } else { next = uri; } return this; } - public void remove(URI uri){ - if(Objects.equals(next,uri)){ + public void remove(URI uri) { + if (Objects.equals(next, uri)) { next = null; - }else{ + } else { next.remove(uri); } } @Override - public String toString(){ + public String toString() { List list = new ArrayList<>(); appendTo(list); StringBuilder builder = new StringBuilder(); for (String text : list) { - if(StringHelper.nonBlank(text)){ + if (StringHelper.nonBlank(text)) { builder.append("/"); builder.append(text); } @@ -59,11 +59,11 @@ public class URI { return builder.toString(); } - private void appendTo(List list){ - if(Objects.nonNull(text)){ + private void appendTo(List list) { + if (Objects.nonNull(text)) { list.addAll(Lists.newArrayList(text.split("/"))); } - if(next!=null){ + if (next != null) { next.appendTo(list); } } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java index 00da4e2..dcbd1e5 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Section.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import java.util.*; @@ -44,12 +45,10 @@ public class Section extends Node { * The Query parameter. */ boolean queryParameter = true; - /** * The Request rows. */ Map requestRows = new LinkedHashMap<>(); - /** * The Out headers. */ @@ -67,6 +66,13 @@ public class Section extends Node { */ Object rawResponse; + public void setUri(String uri) { + String value = ""; + if (StringUtils.isNotBlank(uri)) { + value = uri.replaceAll("\\{", ":").replaceAll("}", ""); + } + this.uri = value; + } /** * Add request row. diff --git a/apidoc-core/src/main/resources/templates/markdown.ftl b/apidoc-core/src/main/resources/templates/markdown.ftl index d258fc7..814d6dd 100644 --- a/apidoc-core/src/main/resources/templates/markdown.ftl +++ b/apidoc-core/src/main/resources/templates/markdown.ftl @@ -72,10 +72,10 @@ ${section.getResponseString()} <#-- 响应参数table列表--> <#if section.responseRows?? && (section.responseRows?size>0)> -| 字段 | 类型 | 描述 | -| :------- | :----- | :---------- | +| 字段 | 类型 | 默认值 | 描述 | +| :------- | :----- |:----- | :---------- | <#list section.responseRows as rowKey,rowValue> -| ${rowValue.key!''} | ${rowValue.type!''} | ${rowValue.remark!''} | +| ${rowValue.key!''} | ${rowValue.type!''} | ${rowValue.def!''} | ${rowValue.remark!''} | diff --git a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Query.java b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Query.java index fe8ab31..81ab609 100644 --- a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Query.java +++ b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/common/Query.java @@ -15,6 +15,6 @@ public class Query { /** * 查询关键字 */ - String q = ""; + String q = "123"; } diff --git a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/UserController.java similarity index 74% rename from apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java rename to apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/UserController.java index 3a54ae5..d7d4c68 100644 --- a/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/KimUserController.java +++ b/apidoc-springmvc/src/test/java/com/kim/apidoc/example/spring/advanced/UserController.java @@ -3,8 +3,6 @@ package com.kim.apidoc.example.spring.advanced; import com.kim.apidoc.example.annotation.KimController; import com.kim.apidoc.example.common.Query; import com.kim.apidoc.example.common.User; -import com.kim.apidoc.example.common.ResultData; -import com.kim.apidoc.example.common.ResultData; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -21,18 +19,18 @@ import org.springframework.web.bind.annotation.RequestMapping; */ @KimController @RequestMapping("/api/v1") -public class KimUserController { +public class UserController { /** - * Detail user. + * 查看用户详情 * - * @param id the id - * @param query the query + * @param userId 用户编号 + * @param query 过滤条件 * @return 用户对象 user */ - @GetMapping(value = "/{id}") - public User detail(@PathVariable String id, Query query) { + @GetMapping(value = "/{userId}") + public User detail(@PathVariable String userId, Query query) { User user = new User(); return user; } -- GitLab