Skip to content

Commit

Permalink
[type:refactor]Split the document field of the apidoc detail interfac…
Browse files Browse the repository at this point in the history
…e,and add fields such as requestHeaders and responseParameters. (#4865)

* apidoc sql

* refact

* commit

* [Task] Shenyu-admin: Fix API document failed to build because of NPE.

* [Task] Shenyu-admin: Fix API document failed to build because of NPE.

* solve conficts,modify LICENSE.

* delete useless code.

* delete useless code.

* commit

* [ISSUE #3843]admin apidoc fix: the required attribute prompt is incorrect when micro service parameter uses "@ApiModelProperty".

* commit

* [shenyu-examples]add swagger to the example project to test the apidoc function of the gateway management system.

* commit

* commit

* commit

* [ISSUE #4690]Supports gzip compression in response to HTTP requests.

* [examples]Add Swagger sample project to demonstrate automatic pull interface documentation.

* delete exapmple

* delete useless code.

* delete useless code.

* swagger apidoc from local cache to database persistence.

* Storage adjustment for swagger type documents :from local cache to database persistence.

* fix get httpMethod

* Fix unit testing.

* Fix unit testing.

* The field splitting of apidoc details API, add requestHeaders, responseParameters, and more.

* Fix unit testing.

* code style.

* Fix unit testing.

* delete unused imports.

* fix JSON data for testing.

* commit

* commit

* commit

---------

Co-authored-by: lianjunwei <lianjunwei@didiglobal.com>
Co-authored-by: dragon-zhang <zhangzicheng@apache.org>
Co-authored-by: xiaoyu <xiaoyu@apache.org>
  • Loading branch information
4 people authored Jul 18, 2023
1 parent 327bf65 commit 62629e5
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.shenyu.admin.model.vo;

import org.apache.shenyu.admin.model.bean.CustomCode;
import org.apache.shenyu.admin.model.bean.DocParameter;
import org.apache.shenyu.admin.model.entity.ApiDO;

import java.io.Serializable;
Expand Down Expand Up @@ -105,6 +107,14 @@ public class ApiVO implements Serializable {
*/
private String documentMd5;

private List<DocParameter> requestHeaders;

private List<DocParameter> requestParameters;

private List<DocParameter> responseParameters;

private List<CustomCode> bizCustomCodeList;

/**
* create time.
*/
Expand Down Expand Up @@ -390,6 +400,76 @@ public void setDocumentMd5(final String documentMd5) {
this.documentMd5 = documentMd5;
}

/**
* get requestHeaders.
*
* @return request headers
*/
public List<DocParameter> getRequestHeaders() {
return requestHeaders;
}

/**
* set requestHeaders.
*
* @param requestHeaders requestHeaders
*/
public void setRequestHeaders(final List<DocParameter> requestHeaders) {
this.requestHeaders = requestHeaders;
}

/**
* get requestParameters.
*
* @return List
*/
public List<DocParameter> getRequestParameters() {
return requestParameters;
}

/**
* set requestParameters.
*
* @param requestParameters requestParameters
*/
public void setRequestParameters(final List<DocParameter> requestParameters) {
this.requestParameters = requestParameters;
}

/**
* get responseParameters.
*
* @return List
*/
public List<DocParameter> getResponseParameters() {
return responseParameters;
}

/**
* set responseParameters.
*
* @param responseParameters responseParameters
*/
public void setResponseParameters(final List<DocParameter> responseParameters) {
this.responseParameters = responseParameters;
}

/**
* get bizCustomCodeList.
* @return List
*/
public List<CustomCode> getBizCustomCodeList() {
return bizCustomCodeList;
}

/**
* set bizCustomCodeList.
* @param bizCustomCodeList bizCustomCodeList
*/
public void setBizCustomCodeList(final List<CustomCode> bizCustomCodeList) {
this.bizCustomCodeList = bizCustomCodeList;
}

/**
* getDateCreated.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shenyu.admin.mapper.ApiMapper;
import org.apache.shenyu.admin.mapper.TagMapper;
import org.apache.shenyu.admin.mapper.TagRelationMapper;
import org.apache.shenyu.admin.model.bean.DocItem;
import org.apache.shenyu.admin.model.dto.ApiDTO;
import org.apache.shenyu.admin.model.entity.ApiDO;
import org.apache.shenyu.admin.model.entity.TagDO;
Expand All @@ -37,6 +38,8 @@
import org.apache.shenyu.admin.model.vo.RuleVO;
import org.apache.shenyu.admin.model.vo.TagVO;
import org.apache.shenyu.admin.service.ApiService;
import org.apache.shenyu.common.enums.ApiSourceEnum;
import org.apache.shenyu.common.utils.JsonUtils;
import org.apache.shenyu.common.utils.ListUtil;
import org.apache.shenyu.admin.service.MetaDataService;
import org.apache.shenyu.admin.service.RuleService;
Expand Down Expand Up @@ -65,11 +68,11 @@
public class ApiServiceImpl implements ApiService {

private final SelectorService selectorService;

private final RuleService ruleService;

private final MetaDataService metaDataService;

private final ApiMapper apiMapper;

private final TagRelationMapper tagRelationMapper;
Expand Down Expand Up @@ -159,7 +162,7 @@ private String create(final ApiDTO apiDTO) {
}
return ShenyuResultMessage.CREATE_SUCCESS;
}

private void unregister(final ApiDO apiDO) {
final String path = apiDO.getApiPath();
RuleQueryCondition condition = new RuleQueryCondition();
Expand All @@ -185,7 +188,7 @@ private void unregister(final ApiDO apiDO) {
Optional.ofNullable(metaDataService.findByPath(path))
.ifPresent(metaDataDO -> metaDataService.delete(Lists.newArrayList(metaDataDO.getId())));
}

private void register(final ApiDO apiDO) {
//register selector/rule/metadata if necessary
final ApiDocRegisterDTO.ApiExt ext = GsonUtils.getInstance().fromJson(apiDO.getExt(), ApiDocRegisterDTO.ApiExt.class);
Expand Down Expand Up @@ -220,7 +223,7 @@ private void register(final ApiDO apiDO) {
.rpcType(apiDO.getRpcType())
.build());
}

@Override
@Transactional(rollbackFor = Exception.class)
public String delete(final List<String> ids) {
Expand All @@ -243,12 +246,21 @@ public ApiVO findById(final String id) {
return Optional.ofNullable(apiMapper.selectByPrimaryKey(id)).map(item -> {
List<TagRelationDO> tagRelations = tagRelationMapper.selectByQuery(TagRelationQuery.builder().apiId(item.getId()).build());
List<String> tagIds = tagRelations.stream().map(TagRelationDO::getTagId).collect(Collectors.toList());
List<TagVO> tagVOS = Lists.newArrayList();
List<TagVO> tagVOs = Lists.newArrayList();
if (CollectionUtils.isNotEmpty(tagIds)) {
List<TagDO> tagDOS = tagMapper.selectByIds(tagIds);
tagVOS = tagDOS.stream().map(TagVO::buildTagVO).collect(Collectors.toList());
tagVOs = tagDOS.stream().map(TagVO::buildTagVO).collect(Collectors.toList());
}
return ApiVO.buildApiVO(item, tagVOS);
ApiVO apiVO = ApiVO.buildApiVO(item, tagVOs);
if (apiVO.getApiSource().equals(ApiSourceEnum.SWAGGER.getValue())) {
DocItem docItem = JsonUtils.jsonToObject(apiVO.getDocument(), DocItem.class);
apiVO.setRequestHeaders(docItem.getRequestHeaders());
apiVO.setRequestParameters(docItem.getRequestParameters());
apiVO.setResponseParameters(docItem.getResponseParameters());
apiVO.setBizCustomCodeList(docItem.getBizCodeList());
}
return apiVO;

}).orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void setUp() {
.rpcType("/dubbo")
.state(0)
.apiSource(0)
.document("document")
.document("{}")
.build();
SpringBeanUtils.getInstance().setApplicationContext(mock(ConfigurableApplicationContext.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,17 @@ private ApiDTO buildApiDTO(final String id) {
apiDTO.setApiOwner("string");
apiDTO.setApiDesc("string");
apiDTO.setApiSource(0);
apiDTO.setDocument("document");
apiDTO.setDocument("{}");
return apiDTO;
}

private void testUpdate(final String id) {
ApiDTO apiDTO = new ApiDTO();
apiDTO.setId(id);
apiDTO.setApiPath("test");
apiDTO.setDocument("testDocument");
apiDTO.setDocument("{\n"
+ " \"module\":\"http-test-controller\"\n"
+ "}");
assertEquals(ShenyuResultMessage.UPDATE_SUCCESS, this.apiService.createOrUpdate(apiDTO));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public class SwaggerDocParserTest {
+ " }\n"
+ " },\n"
+ " \"deprecated\":false\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " \"/test/payment\":{\n"
+ " \"post\":{\n"
Expand Down Expand Up @@ -142,7 +143,6 @@ public class SwaggerDocParserTest {
+ " },\n"
+ " \"deprecated\":false\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " \"definitions\":{\n"
Expand Down Expand Up @@ -200,6 +200,6 @@ public void testParseJson() {
JsonObject docRoot = GsonUtils.getInstance().fromJson(DOC_INFO_JSON, JsonObject.class);
docRoot.addProperty("basePath", "/" + "testClusterName");
DocInfo docInfo = swaggerDocParser.parseJson(docRoot);
assert docInfo.getDocModuleList().get(0).getModule().equals("shenyu-examples-http-swagger2 API");
assert docInfo.getDocModuleList().get(0).getModule().equals("Order API");
}
}

0 comments on commit 62629e5

Please sign in to comment.