Skip to content

Commit

Permalink
Improve resource order in synpase artifact according to API payload
Browse files Browse the repository at this point in the history
  • Loading branch information
tharikaGitHub committed Sep 29, 2020
1 parent 7f61361 commit d1fc8ac
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.wso2.carbon.apimgt.api.model.URITemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -113,11 +113,11 @@ public List<String> getPathParamNames(String uriTemplate) {
* @return a structured uri template map using provided Swagger Data Resource Paths
*/
public Map<String, Map<String, SwaggerData.Resource>> getResourceMap(SwaggerData swaggerData) {
Map<String, Map<String, SwaggerData.Resource>> uriTemplateMap = new HashMap<>();
Map<String, Map<String, SwaggerData.Resource>> uriTemplateMap = new LinkedHashMap<>();
for (SwaggerData.Resource resource : swaggerData.getResources()) {
Map<String, SwaggerData.Resource> resources = uriTemplateMap.get(resource.getPath());
if (resources == null) {
resources = new HashMap<>();
resources = new LinkedHashMap<>();
uriTemplateMap.put(resource.getPath(), resources);
}
resources.put(resource.getVerb().toUpperCase(), resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -116,7 +117,7 @@ public void setScopes(List<Scope> scopes) {
private String transportType;
private String security;
private String apiLevelPolicy;
private Set<Resource> resources = new HashSet<>();
private Set<Resource> resources = new LinkedHashSet<>();
private Set<Scope> scopes = new HashSet<>();

public SwaggerData(API api) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7349,7 +7349,7 @@ public HashMap<String, String> getURITemplatesPerAPIAsString(APIIdentifier ident

public Set<URITemplate> getURITemplatesOfAPI(APIIdentifier identifier)
throws APIManagementException {
Map<Integer, URITemplate> uriTemplates = new HashMap<>();
Map<Integer, URITemplate> uriTemplates = new LinkedHashMap<>();
Map<Integer, Set<String>> scopeToURITemplateId = new HashMap<>();

try (Connection conn = APIMgtDBUtil.getConnection();
Expand Down Expand Up @@ -7410,7 +7410,7 @@ public Set<URITemplate> getURITemplatesOfAPI(APIIdentifier identifier)
handleException("Failed to get URI Templates of API" + identifier, e);
}

return new HashSet<>(uriTemplates.values());
return new LinkedHashSet<>(uriTemplates.values());
}

private void setAssociatedAPIProducts(APIIdentifier identifier, Map<Integer, URITemplate> uriTemplates)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,8 @@ public class SQLConstants {
" WHERE " +
" API.API_PROVIDER = ? AND " +
" API.API_NAME = ? AND " +
" API.API_VERSION = ? ";
" API.API_VERSION = ? " +
" ORDER BY AUM.URL_MAPPING_ID ASC ";

public static final String GET_API_PRODUCT_URI_TEMPLATE_ASSOCIATION_SQL =
" SELECT " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,33 @@ private String generateAPIDefinition(SwaggerData swaggerData, Swagger swaggerObj
if (StringUtils.isEmpty(swaggerObj.getInfo().getVersion())) {
swaggerObj.getInfo().setVersion(swaggerData.getVersion());
}
preserveResourcePathOrderFromAPI(swaggerData, swaggerObj);
return getSwaggerJsonString(swaggerObj);
}

/**
* Preserve and rearrange the Swagger definition according to the resource path order of the updating API payload.
*
* @param swaggerData Updating API swagger data
* @param swaggerObj Updated Swagger definition
*/
private void preserveResourcePathOrderFromAPI(SwaggerData swaggerData, Swagger swaggerObj) {

Set<String> orderedResourcePaths = new LinkedHashSet<>();
Map<String, Path> orderedSwaggerPaths = new LinkedHashMap<>();
// Iterate the URI template order given in the updating API payload (Swagger Data) and rearrange resource paths
// order in OpenAPI with relevance to the first matching resource path item from the swagger data path list.
for (SwaggerData.Resource resource : swaggerData.getResources()) {
String path = resource.getPath();
if (!orderedResourcePaths.contains(path)) {
orderedResourcePaths.add(path);
// Get the resource path item for the path from existing Swagger
orderedSwaggerPaths.put(path, swaggerObj.getPath(path));
}
}
swaggerObj.setPaths(orderedSwaggerPaths);
}

/**
* This method validates the given OpenAPI definition by content
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,34 @@ private String generateAPIDefinition(SwaggerData swaggerData, OpenAPI openAPI) t
if (StringUtils.isEmpty(openAPI.getInfo().getVersion())) {
openAPI.getInfo().setVersion(swaggerData.getVersion());
}
preserveResourcePathOrderFromAPI(swaggerData, openAPI);
return Json.pretty(openAPI);
}

/**
* Preserve and rearrange the OpenAPI definition according to the resource path order of the updating API payload.
*
* @param swaggerData Updating API swagger data
* @param openAPI Updated OpenAPI definition
*/
private void preserveResourcePathOrderFromAPI(SwaggerData swaggerData, OpenAPI openAPI) {

Set<String> orderedResourcePaths = new LinkedHashSet<>();
Paths orderedOpenAPIPaths = new Paths();
// Iterate the URI template order given in the updating API payload (Swagger Data) and rearrange resource paths
// order in OpenAPI with relevance to the first matching resource path item from the swagger data path list.
for (SwaggerData.Resource resource : swaggerData.getResources()) {
String path = resource.getPath();
if (!orderedResourcePaths.contains(path)) {
orderedResourcePaths.add(path);
// Get the resource path item for the path from existing OpenAPI
PathItem resourcePathItem = openAPI.getPaths().get(path);
orderedOpenAPIPaths.addPathItem(path, resourcePathItem);
}
}
openAPI.setPaths(orderedOpenAPIPaths);
}

/**
* Construct path parameters to the Operation
*
Expand Down

0 comments on commit d1fc8ac

Please sign in to comment.