Fix Postman FormData description

This commit is contained in:
oppofind 2020-11-18 13:16:13 +08:00
parent 61d25f8e9a
commit 79c206a40d
4 changed files with 329 additions and 329 deletions

View File

@ -1,193 +1,193 @@
/*
* smart-doc https://github.com/shalousun/smart-doc
*
* Copyright (C) 2018-2020 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.helper;
import com.power.common.util.RandomUtil;
import com.power.common.util.StringUtil;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.DocTags;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.DocJavaField;
import com.power.doc.model.FormData;
import com.power.doc.utils.DocClassUtil;
import com.power.doc.utils.DocUtil;
import com.power.doc.utils.JavaClassUtil;
import com.power.doc.utils.JavaClassValidateUtil;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
/**
* @author yu 2019/12/25.
*/
public class FormDataBuildHelper {
/**
* build form data
*
* @param className class name
* @param registryClasses Class container
* @param counter invoked counter
* @param builder ProjectDocConfigBuilder
* @param pre pre
* @return list of FormData
*/
public static List<FormData> getFormData(String className, Map<String, String> registryClasses, int counter, ProjectDocConfigBuilder builder, String pre) {
if (StringUtil.isEmpty(className)) {
throw new RuntimeException("Class name can't be null or empty.");
}
ApiConfig apiConfig = builder.getApiConfig();
List<FormData> formDataList = new ArrayList<>();
if (counter > apiConfig.getRecursionLimit()) {
return formDataList;
}
// Check circular reference
if (registryClasses.containsKey(className) && counter > registryClasses.size()) {
return formDataList;
}
// Registry class
registryClasses.put(className, className);
counter++;
boolean skipTransientField = apiConfig.isSkipTransientField();
boolean requestFieldToUnderline = apiConfig.isRequestFieldToUnderline();
boolean responseFieldToUnderline = apiConfig.isResponseFieldToUnderline();
String simpleName = DocClassUtil.getSimpleName(className);
String[] globGicName = DocClassUtil.getSimpleGicName(className);
JavaClass cls = builder.getJavaProjectBuilder().getClassByName(simpleName);
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new HashSet<>());
if (JavaClassValidateUtil.isPrimitive(simpleName)) {
FormData formData = new FormData();
formData.setKey(pre);
formData.setType("text");
formData.setValue(StringUtil.removeQuotes(RandomUtil.randomValueByType(className)));
formDataList.add(formData);
return formDataList;
}
if (JavaClassValidateUtil.isCollection(simpleName) || JavaClassValidateUtil.isArray(simpleName)) {
String gicName = globGicName[0];
if (JavaClassValidateUtil.isArray(gicName)) {
gicName = gicName.substring(0, gicName.indexOf("["));
}
formDataList.addAll(getFormData(gicName, registryClasses, counter, builder, pre + "[]"));
}
int n = 0;
out:
for (DocJavaField docField : fields) {
JavaField field = docField.getJavaField();
String fieldName = field.getName();
String subTypeName = docField.getFullyQualifiedName();
String fieldGicName = docField.getGenericCanonicalName();
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(subTypeName);
if (field.isStatic() || "this$0".equals(fieldName) ||
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if (field.isTransient() && skipTransientField) {
continue;
}
if (responseFieldToUnderline || requestFieldToUnderline) {
fieldName = StringUtil.camelToUnderline(fieldName);
}
Map<String, String> tagsMap = DocUtil.getFieldTagsValue(field, docField);
if (tagsMap.containsKey(DocTags.IGNORE)) {
continue out;
}
String typeSimpleName = field.getType().getSimpleName();
if (JavaClassValidateUtil.isMap(subTypeName)) {
continue;
}
String comment = docField.getComment();
if (StringUtil.isNotEmpty(comment)) {
comment = DocUtil.replaceNewLineToHtmlBr(comment);
}
if (fieldGicName.contains(DocGlobalConstants.MULTIPART_FILE_FULLY)) {
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("file");
formData.setDesc(comment);
formData.setValue("");
formDataList.add(formData);
} else if (JavaClassValidateUtil.isPrimitive(subTypeName)) {
String fieldValue = "";
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
fieldValue = tagsMap.get(DocTags.MOCK);
} else {
fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
}
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");
formData.setValue(StringUtil.removeQuotes(fieldValue));
formData.setDesc(comment);
formDataList.add(formData);
} else if (javaClass.isEnum()) {
Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE);
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");
formData.setValue(StringUtil.removeQuotes(String.valueOf(value)));
formData.setDesc(comment);
formDataList.add(formData);
} else if (JavaClassValidateUtil.isCollection(subTypeName)) {
String gNameTemp = field.getType().getGenericCanonicalName();
String[] gNameArr = DocClassUtil.getSimpleGicName(gNameTemp);
if (gNameArr.length == 0) {
continue out;
}
String gName = DocClassUtil.getSimpleGicName(gNameTemp)[0];
if (!JavaClassValidateUtil.isPrimitive(gName)) {
if (!simpleName.equals(gName) && !gName.equals(simpleName)) {
if (gName.length() == 1) {
int len = globGicName.length;
if (len > 0) {
String gicName = (n < len) ? globGicName[n] : globGicName[len - 1];
if (!JavaClassValidateUtil.isPrimitive(gicName) && !simpleName.equals(gicName)) {
formDataList.addAll(getFormData(gicName, registryClasses, counter, builder, pre + fieldName + "[0]."));
}
}
} else {
formDataList.addAll(getFormData(gName, registryClasses, counter, builder, pre + fieldName + "[0]."));
}
}
}
} else if (subTypeName.length() == 1 || DocGlobalConstants.JAVA_OBJECT_FULLY.equals(subTypeName)) {
// For Generics,do nothing, spring mvc not support
// if (n < globGicName.length) {
// String gicName = globGicName[n];
// formDataList.addAll(getFormData(gicName, registryClasses, counter, builder, pre + fieldName + "."));
// }
// n++;
continue;
} else {
formDataList.addAll(getFormData(fieldGicName, registryClasses, counter, builder, pre + fieldName + "."));
}
}
return formDataList;
}
}
/*
* smart-doc https://github.com/shalousun/smart-doc
*
* Copyright (C) 2018-2020 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.helper;
import com.power.common.util.RandomUtil;
import com.power.common.util.StringUtil;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.DocTags;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.DocJavaField;
import com.power.doc.model.FormData;
import com.power.doc.utils.DocClassUtil;
import com.power.doc.utils.DocUtil;
import com.power.doc.utils.JavaClassUtil;
import com.power.doc.utils.JavaClassValidateUtil;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
/**
* @author yu 2019/12/25.
*/
public class FormDataBuildHelper {
/**
* build form data
*
* @param className class name
* @param registryClasses Class container
* @param counter invoked counter
* @param builder ProjectDocConfigBuilder
* @param pre pre
* @return list of FormData
*/
public static List<FormData> getFormData(String className, Map<String, String> registryClasses, int counter, ProjectDocConfigBuilder builder, String pre) {
if (StringUtil.isEmpty(className)) {
throw new RuntimeException("Class name can't be null or empty.");
}
ApiConfig apiConfig = builder.getApiConfig();
List<FormData> formDataList = new ArrayList<>();
if (counter > apiConfig.getRecursionLimit()) {
return formDataList;
}
// Check circular reference
if (registryClasses.containsKey(className) && counter > registryClasses.size()) {
return formDataList;
}
// Registry class
registryClasses.put(className, className);
counter++;
boolean skipTransientField = apiConfig.isSkipTransientField();
boolean requestFieldToUnderline = apiConfig.isRequestFieldToUnderline();
boolean responseFieldToUnderline = apiConfig.isResponseFieldToUnderline();
String simpleName = DocClassUtil.getSimpleName(className);
String[] globGicName = DocClassUtil.getSimpleGicName(className);
JavaClass cls = builder.getJavaProjectBuilder().getClassByName(simpleName);
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new HashSet<>());
if (JavaClassValidateUtil.isPrimitive(simpleName)) {
FormData formData = new FormData();
formData.setKey(pre);
formData.setType("text");
formData.setValue(StringUtil.removeQuotes(RandomUtil.randomValueByType(className)));
formDataList.add(formData);
return formDataList;
}
if (JavaClassValidateUtil.isCollection(simpleName) || JavaClassValidateUtil.isArray(simpleName)) {
String gicName = globGicName[0];
if (JavaClassValidateUtil.isArray(gicName)) {
gicName = gicName.substring(0, gicName.indexOf("["));
}
formDataList.addAll(getFormData(gicName, registryClasses, counter, builder, pre + "[]"));
}
int n = 0;
out:
for (DocJavaField docField : fields) {
JavaField field = docField.getJavaField();
String fieldName = field.getName();
String subTypeName = docField.getFullyQualifiedName();
String fieldGicName = docField.getGenericCanonicalName();
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(subTypeName);
if (field.isStatic() || "this$0".equals(fieldName) ||
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if (field.isTransient() && skipTransientField) {
continue;
}
if (responseFieldToUnderline || requestFieldToUnderline) {
fieldName = StringUtil.camelToUnderline(fieldName);
}
Map<String, String> tagsMap = DocUtil.getFieldTagsValue(field, docField);
if (tagsMap.containsKey(DocTags.IGNORE)) {
continue out;
}
String typeSimpleName = field.getType().getSimpleName();
if (JavaClassValidateUtil.isMap(subTypeName)) {
continue;
}
String comment = docField.getComment();
if (StringUtil.isNotEmpty(comment)) {
comment = DocUtil.replaceNewLineToHtmlBr(comment);
}
if (fieldGicName.contains(DocGlobalConstants.MULTIPART_FILE_FULLY)) {
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("file");
formData.setDescription(comment);
formData.setValue("");
formDataList.add(formData);
} else if (JavaClassValidateUtil.isPrimitive(subTypeName)) {
String fieldValue = "";
if (tagsMap.containsKey(DocTags.MOCK) && StringUtil.isNotEmpty(tagsMap.get(DocTags.MOCK))) {
fieldValue = tagsMap.get(DocTags.MOCK);
} else {
fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName());
}
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");
formData.setValue(StringUtil.removeQuotes(fieldValue));
formData.setDescription(comment);
formDataList.add(formData);
} else if (javaClass.isEnum()) {
Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE);
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");
formData.setValue(StringUtil.removeQuotes(String.valueOf(value)));
formData.setDescription(comment);
formDataList.add(formData);
} else if (JavaClassValidateUtil.isCollection(subTypeName)) {
String gNameTemp = field.getType().getGenericCanonicalName();
String[] gNameArr = DocClassUtil.getSimpleGicName(gNameTemp);
if (gNameArr.length == 0) {
continue out;
}
String gName = DocClassUtil.getSimpleGicName(gNameTemp)[0];
if (!JavaClassValidateUtil.isPrimitive(gName)) {
if (!simpleName.equals(gName) && !gName.equals(simpleName)) {
if (gName.length() == 1) {
int len = globGicName.length;
if (len > 0) {
String gicName = (n < len) ? globGicName[n] : globGicName[len - 1];
if (!JavaClassValidateUtil.isPrimitive(gicName) && !simpleName.equals(gicName)) {
formDataList.addAll(getFormData(gicName, registryClasses, counter, builder, pre + fieldName + "[0]."));
}
}
} else {
formDataList.addAll(getFormData(gName, registryClasses, counter, builder, pre + fieldName + "[0]."));
}
}
}
} else if (subTypeName.length() == 1 || DocGlobalConstants.JAVA_OBJECT_FULLY.equals(subTypeName)) {
// For Generics,do nothing, spring mvc not support
// if (n < globGicName.length) {
// String gicName = globGicName[n];
// formDataList.addAll(getFormData(gicName, registryClasses, counter, builder, pre + fieldName + "."));
// }
// n++;
continue;
} else {
formDataList.addAll(getFormData(fieldGicName, registryClasses, counter, builder, pre + fieldName + "."));
}
}
return formDataList;
}
}

View File

@ -1,85 +1,85 @@
/*
* smart-doc
*
* Copyright (C) 2018-2020 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.model;
/**
* @author xingzi 2019/12/21 20:20
*/
public class FormData {
private String key;
private String type;
private String desc;
private String src;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "FormData{" +
"key='" + key + '\'' +
", type='" + type + '\'' +
", desc='" + desc + '\'' +
", src='" + src + '\'' +
", value='" + value + '\'' +
'}';
}
}
/*
* smart-doc
*
* Copyright (C) 2018-2020 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.model;
/**
* @author xingzi 2019/12/21 20:20
*/
public class FormData {
private String key;
private String type;
private String description;
private String src;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "FormData{" +
"key='" + key + '\'' +
", type='" + type + '\'' +
", description='" + description + '\'' +
", src='" + src + '\'' +
", value='" + value + '\'' +
'}';
}
}

View File

@ -1,47 +1,47 @@
/*
* smart-doc https://github.com/shalousun/smart-doc
*
* Copyright (C) 2018-2020 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.model.postman;
import com.power.common.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.UUID;
/**
* @author xingzi
*/
public class InfoBean {
String schema;
private String _postman_id = UUID.randomUUID().toString();
private String name;
public InfoBean(String name) {
if (StringUtils.isBlank(name)) {
this.name = "smart-doc " + DateTimeUtil.long2Str(System.currentTimeMillis(),DateTimeUtil.DATE_FORMAT_SECOND);
} else {
this.name = name;
}
this.schema = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json";
}
}
/*
* smart-doc https://github.com/shalousun/smart-doc
*
* Copyright (C) 2018-2020 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.model.postman;
import com.power.common.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.UUID;
/**
* @author xingzi
*/
public class InfoBean {
String schema;
private String _postman_id = UUID.randomUUID().toString();
private String name;
public InfoBean(String name) {
if (StringUtils.isBlank(name)) {
this.name = "smart-doc " + DateTimeUtil.long2Str(System.currentTimeMillis(),DateTimeUtil.DATE_FORMAT_SECOND);
} else {
this.name = name;
}
this.schema = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json";
}
}

View File

@ -351,13 +351,13 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
FormData formData = new FormData();
formData.setKey(paramName);
formData.setType("file");
formData.setDesc(comment);
formData.setDescription(comment);
formData.setValue(mockValue);
formDataList.add(formData);
} else if (JavaClassValidateUtil.isPrimitive(typeName)) {
FormData formData = new FormData();
formData.setKey(paramName);
formData.setDesc(comment);
formData.setDescription(comment);
formData.setType("text");
formData.setValue(mockValue);
formDataList.add(formData);
@ -375,7 +375,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
if (!paramName.contains("[]")) {
formData.setKey(paramName + "[]");
}
formData.setDesc(comment);
formData.setDescription(comment);
formData.setType("text");
formData.setValue(RandomUtil.randomValueByType(gicName));
formDataList.add(formData);
@ -386,7 +386,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
FormData formData = new FormData();
formData.setKey(paramName);
formData.setType("text");
formData.setDesc(comment);
formData.setDescription(comment);
formData.setValue(strVal);
formDataList.add(formData);
} else {