Skip to content

Commit 345edbc

Browse files
committed
接口同步
1 parent 78c03c5 commit 345edbc

File tree

3 files changed

+134
-18
lines changed

3 files changed

+134
-18
lines changed
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.ztianzeng.apidoc.utils;
22

33

4-
import org.apache.commons.lang3.StringUtils;
5-
import org.apache.commons.lang3.tuple.ImmutablePair;
6-
import org.apache.commons.lang3.tuple.Pair;
7-
84
public class RefUtils {
95

106
public static String constructRef(String simpleRef) {
@@ -15,15 +11,8 @@ public static String constructRef(String simpleRef, String prefix) {
1511
return prefix + simpleRef;
1612
}
1713

18-
public static Pair extractSimpleName(String ref) {
19-
int idx = ref.lastIndexOf("/");
20-
if (idx > 0) {
21-
String simple = ref.substring(idx + 1);
22-
if (!StringUtils.isEmpty(simple)) {
23-
return new ImmutablePair<>(simple, ref.substring(0, idx + 1));
24-
}
25-
}
26-
return new ImmutablePair<>(ref, null);
14+
public static String getRef(String ref) {
15+
return ref.replace("#/components/schemas/", "");
2716

2817
}
2918
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.ztianzeng.apidoc.yapi.module;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @description: kv
8+
* @author: chengsheng@qbb6.com
9+
* @date: 2018/10/27
10+
*/
11+
public class KV<K, V> extends LinkedHashMap<K, V> {
12+
public <K, V> KV() {
13+
}
14+
15+
public static <K, V> KV by(K key, V value) {
16+
return new KV().set(key, value);
17+
}
18+
19+
public static <K, V> KV create() {
20+
return new KV();
21+
}
22+
23+
public KV set(K key, V value) {
24+
super.put(key, value);
25+
return this;
26+
}
27+
28+
public KV set(Map map) {
29+
super.putAll(map);
30+
return this;
31+
}
32+
33+
public KV set(KV KV) {
34+
super.putAll(KV);
35+
return this;
36+
}
37+
38+
public KV delete(Object key) {
39+
super.remove(key);
40+
return this;
41+
}
42+
43+
public <T> T getAs(Object key) {
44+
return (T) get(key);
45+
}
46+
47+
public String getStr(Object key) {
48+
return (String) get(key);
49+
}
50+
51+
public Integer getInt(Object key) {
52+
return (Integer) get(key);
53+
}
54+
55+
public Long getLong(Object key) {
56+
return (Long) get(key);
57+
}
58+
59+
public Boolean getBoolean(Object key) {
60+
return (Boolean) get(key);
61+
}
62+
63+
public Float getFloat(Object key) {
64+
return (Float) get(key);
65+
}
66+
67+
68+
/**
69+
* key 存在,并且 value 不为 null
70+
*/
71+
public boolean notNull(Object key) {
72+
return get(key) != null;
73+
}
74+
75+
/**
76+
* key 不存在,或者 key 存在但 value 为null
77+
*/
78+
public boolean isNull(Object key) {
79+
return get(key) == null;
80+
}
81+
82+
/**
83+
* key 存在,并且 value 为 true,则返回 true
84+
*/
85+
public boolean isTrue(Object key) {
86+
Object value = get(key);
87+
return (value instanceof Boolean && ((Boolean) value == true));
88+
}
89+
90+
/**
91+
* key 存在,并且 value 为 false,则返回 true
92+
*/
93+
public boolean isFalse(Object key) {
94+
Object value = get(key);
95+
return (value instanceof Boolean && ((Boolean) value == false));
96+
}
97+
98+
99+
}

apidoc-yapi-plugin/src/main/java/com/ztianzeng/apidoc/yapi/upload/UploadToYapi.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import com.ztianzeng.apidoc.models.OpenAPI;
77
import com.ztianzeng.apidoc.models.Operation;
88
import com.ztianzeng.apidoc.models.Paths;
9+
import com.ztianzeng.apidoc.models.media.Schema;
10+
import com.ztianzeng.apidoc.models.parameters.RequestBody;
911
import com.ztianzeng.apidoc.utils.Json;
12+
import com.ztianzeng.apidoc.utils.RefUtils;
1013
import com.ztianzeng.apidoc.yapi.constant.YapiConstant;
1114
import com.ztianzeng.apidoc.yapi.module.*;
1215
import com.ztianzeng.apidoc.yapi.utils.HttpClientUtil;
@@ -135,19 +138,28 @@ public List<YapiApiDTO> toYapiDTO(OpenAPI openAPI) {
135138
List<YapiApiDTO> apiDTOS = new ArrayList<>(paths.size());
136139

137140
openAPI.getPaths().forEach((k, v) -> {
138-
Operation operation = v.getPost();
139-
if (operation == null) {
140-
operation = v.getGet();
141+
boolean isGet = false;
142+
if (v.getPost() == null) {
143+
isGet = true;
141144
}
145+
Operation operation = isGet ? v.getGet() : v.getPost();
142146

143147
YapiApiDTO yapiApiDTO = new YapiApiDTO();
144148
yapiApiDTO.setDesc(operation.getDescription());
145149

146150

147-
yapiApiDTO.setReq_params(operation.getParameters());
148151
yapiApiDTO.setTag(operation.getTags().stream().findFirst().orElse(""));
149152
try {
150-
yapiApiDTO.setRequestBody(mapper.writeValueAsString(operation.getRequestBody()));
153+
if (isGet) {
154+
yapiApiDTO.setReq_params(operation.getParameters());
155+
} else {
156+
157+
158+
yapiApiDTO.setRequestBody(getRequestBOdy(openAPI, operation.getRequestBody()));
159+
160+
}
161+
162+
151163
yapiApiDTO.setResponse(mapper.writeValueAsString(operation.getResponses()));
152164
} catch (JsonProcessingException e) {
153165
e.printStackTrace();
@@ -230,4 +242,20 @@ private HttpGet getHttpGet(String url) {
230242
return null;
231243
}
232244

245+
public String getRequestBOdy(OpenAPI openAPI, RequestBody requestBody) throws JsonProcessingException {
246+
KV kv = KV.create();
247+
;
248+
if (StringUtils.isNotBlank(requestBody.getContent().get("application/json").getSchema().get$ref())) {
249+
Schema schema = openAPI.getComponents().getSchemas().get(RefUtils.getRef(requestBody.getContent().get("application/json").getSchema().get$ref()));
250+
251+
kv.set("type", schema.getType());
252+
kv.set("description", "1111");
253+
kv.set("items", schema.getTitle());
254+
255+
}
256+
return mapper.writeValueAsString(kv);
257+
258+
259+
}
260+
233261
}

0 commit comments

Comments
 (0)