Skip to content

Commit

Permalink
Refactor: Rename packages to remove circularities
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jan 25, 2025
1 parent 3db1a29 commit ee1e6f0
Show file tree
Hide file tree
Showing 78 changed files with 135 additions and 217 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions net/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</fileset>
<fileset dir="../tdrules-model/target/generated-sources/openapi/src/main/java">
<include name="giis/tdrules/openapi/model/" />
<include name="giis/tdrules/openapi/model/shared/" />
</fileset>
<fileset dir="../tdrules-client/src/main/java" />
<fileset dir="../tdrules-client-rdb/src/main/java" />
Expand Down Expand Up @@ -185,7 +186,7 @@
<replacefilter token="new TreeSet" value="new HashSet" />
</replace>
<replace dir="sharpen-temp/sharpen-temp.net/">
<include name="Giis.Tdrules.Model/ModelUtil.cs" />
<include name="Giis.Tdrules.Model.Shared/ModelUtil.cs" />
<replacefilter token="Java.Util.Collections.EmptyList()" value="new List&lt;T&gt;()" />
<replacefilter token="Java.Util.Collections.EmptyMap()" value="new Dictionary&lt;string, string&gt;()" />
</replace>
Expand Down Expand Up @@ -232,9 +233,9 @@
<include name="Giis.Tdrules.Client.Rdb/**/*" />
<exclude name="Giis.Tdrules.Model.IO/ModelJsonSerializer.cs" />
<include name="Giis.Tdrules.Model.IO/**/*" />
<include name="Giis.Tdrules.Model/ModelUtil.cs" />
<include name="Giis.Tdrules.Model/EntityTypes.cs" />
<include name="Giis.Tdrules.Model/RuleTypes.cs" />
<include name="Giis.Tdrules.Model.Shared/ModelUtil.cs" />
<include name="Giis.Tdrules.Model.Shared/EntityTypes.cs" />
<include name="Giis.Tdrules.Model.Shared/RuleTypes.cs" />
<include name="Giis.Tdrules.Store.Rdb/**/*" />
<include name="Giis.Tdrules.Store.Dtypes/**/*" />
<include name="Giis.Tdrules.Store.Stypes/**/*" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String getMermaid() {
}

private void drawEntityRelations(TdEntity entity) {
for (TdAttribute attribute : giis.tdrules.model.ModelUtil.safe(entity.getAttributes())) {
for (TdAttribute attribute : giis.tdrules.model.shared.ModelUtil.safe(entity.getAttributes())) {
if (attribute.isRid()) {
// relation to another entity is drawn differently if it is from an array
if (entity.isArray()) {
Expand All @@ -76,7 +76,7 @@ private void drawTypeDefinitions(TdEntity entity) {
}

private void drawPostOperations(TdEntity entity) {
for (Ddl operation : giis.tdrules.model.ModelUtil.safe(entity.getDdls()))
for (Ddl operation : giis.tdrules.model.shared.ModelUtil.safe(entity.getDdls()))
drawMethod(entity.getName(), operation.getCommand(), operation.getQuery());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import giis.tdrules.client.oa.shared.OaSchemaLogger;
import giis.tdrules.client.oa.transform.SchemaTransformer;
import giis.tdrules.openapi.model.TdSchema;
import io.swagger.v3.parser.OpenAPIV3Parser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import giis.tdrules.model.OaExtensions;
import giis.tdrules.model.shared.OaExtensions;
import io.swagger.v3.oas.models.media.Schema;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package giis.tdrules.client.oa;
package giis.tdrules.client.oa.shared;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package giis.tdrules.client.oa.transform;
package giis.tdrules.client.oa.shared;

import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import giis.tdrules.model.EntityTypes;
import giis.tdrules.model.OaExtensions;
import giis.tdrules.client.oa.shared.OaUtil;
import giis.tdrules.model.shared.EntityTypes;
import giis.tdrules.model.shared.OaExtensions;
import giis.tdrules.openapi.model.TdAttribute;
import giis.tdrules.openapi.model.TdEntity;
import io.swagger.v3.oas.models.media.ArraySchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,40 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import giis.tdrules.model.ModelUtil;
import giis.tdrules.model.shared.ModelUtil;
import giis.tdrules.openapi.model.Ddl;
import giis.tdrules.openapi.model.TdEntity;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;

/**
* Tuples of paths, path parameters, operations and entities (both request and response)
* from the OpenAPI model, indexed by entity name.
* Currently, does not track if each tuple is related to a request or response
* Determines the path info required by the POST and PUT operations associated to the
* entities in the TdSchema (with a request body that corresponds to some entity
* in the schema).
*
* These operations are scanned and stored internally during instantiation
* and can be later used during the transformation of an entity to determine
* the endpoint where POST for entities must be sent
* and can be later used during the transformation of an entity.
*/
public class PathTransformer {
private static final String POST = "post";
private static final String PUT = "put";
private static final String REQUEST = "request";
private static final String RESPONSE = "response";

protected static final Logger log = LoggerFactory.getLogger(PathTransformer.class);

// Holds the path related data from the oaSchema for each entity in the schema
// (organized by the entity name)
private EntityPaths entityPaths = new EntityPaths();

public class EntityPaths {
Map<String, List<EntityPath>> epaths = new TreeMap<>();

public List<EntityPath> get(String entityName) {
List<EntityPath> items = epaths.get(entityName);
return items == null ? new ArrayList<>() : items;
}
public void add(EntityPath epath) {
List<EntityPath> items = epaths.get(epath.entityName);
if (items == null) { //initialize entry if it is new
items = new ArrayList<>();
epaths.put(epath.entityName, items);
}
if (!containedIn(epath, items)) // Add only if not duplicated
items.add(epath);
}
private boolean containedIn(EntityPath epath, List<EntityPath> epaths) {
for (EntityPath item : epaths)
if (epath.entityName.equals(item.entityName) && epath.method.equals(item.method) && epath.path.equals(item.path))
return true;
return false;
}
}
// (organized by http method)
Map<String, List<EntityPath>> entityPaths = new TreeMap<>();

public class EntityPath {
String entityName;
String method;
String path;
List<Parameter> oaParams;
}

public PathTransformer(Map<String, PathItem> pathItems) {
entityPaths.put("post", new ArrayList<>());
entityPaths.put("put", new ArrayList<>());
this.initialize(pathItems);
}

Expand All @@ -83,90 +51,55 @@ private void initialize(Map<String, PathItem> pathItems) {
if (pathItems == null)
return;
for (Entry<String, PathItem> pathSchema : pathItems.entrySet()) {
addEntityPath(POST, REQUEST, pathSchema.getKey(), pathSchema.getValue());
addEntityPath(PUT, REQUEST, pathSchema.getKey(), pathSchema.getValue());
addEntityPath("post", pathSchema.getKey(), pathSchema.getValue());
addEntityPath("put", pathSchema.getKey(), pathSchema.getValue());
}
}

// Adds an entity path object if it corresponds to a method that accepts a body
// with media type application/json
private void addEntityPath(String method, String requestOrResponse, String oaKey, PathItem oaPath) {
private void addEntityPath(String method, String oaKey, PathItem oaPath) {
log.trace("Check {} endpoint {}", method, oaKey);
Operation operation = getOaOperation(oaPath, method);
Operation operation = null;
if ("post".equals(method))
operation = oaPath.getPost();
else if ("put".equals(method))
operation = oaPath.getPut();

String ref = getBodyEntityRef(operation, requestOrResponse);
String ref = getRequestBodyEntityRef(operation);
if (ref != null) {
log.trace("Found {}, body ref: {}", method, ref);
String entityName = ref.replace("#/components/schemas/", "");
log.debug("Add {} with path {} to entity {}", method, oaKey, entityName);
EntityPath entityPath = new EntityPath();
entityPath.entityName = entityName;
entityPath.method = method;
entityPath.path = oaKey;
entityPath.oaParams = operation.getParameters();
this.entityPaths.add(entityPath);
this.entityPaths.get(method).add(entityPath);
}
}

private String getBodyEntityRef(Operation operation, String requestOrResponse) {
private String getRequestBodyEntityRef(Operation operation) {
if (operation == null)
return null;
Content content = getOaBodyContent(operation, requestOrResponse);
if (content == null)
RequestBody body = operation.getRequestBody();
if (body == null)
return null;
MediaType media = getOaMediaType(content);
MediaType media = body.getContent().get("application/json");
if (media == null)
return null;
Schema<?> schema = media.getSchema();
if (schema == null)
return null;
return schema.get$ref();
}

private Operation getOaOperation(PathItem oaPath, String method) {
if (POST.equalsIgnoreCase(method))
return oaPath.getPost();
else if (PUT.equalsIgnoreCase(method))
return oaPath.getPut();
else
throw new TransformException("Operation not handled for method " + method);
}
private Content getOaBodyContent(Operation operation, String requestOrResponse) {
if (REQUEST.equalsIgnoreCase(requestOrResponse)) {
RequestBody body = operation.getRequestBody();
return body == null ? null : body.getContent();
} else if (RESPONSE.equalsIgnoreCase(requestOrResponse)) {
// must select the first 2xx response using this method, null if not found
return getOaResponseContent(operation);
} else
throw new TransformException("Get body not handled for " + requestOrResponse);
}
private MediaType getOaMediaType(Content content) {
MediaType media = content.get("application/json");
if (media == null) {
// Some specifications (market) do not specify media type for each response,
// but they are read as a media type range */* (could be also application/*), try this
media = content.get("*/*");
}
return media;
}
private Content getOaResponseContent(Operation operation) {
ApiResponses responses = operation.getResponses();
if (responses == null)
return null;
for (Entry<String, ApiResponse> response : responses.entrySet())
if (response.getKey().substring(0, 1).equals("2")) // first 2XX response
return response.getValue().getContent();
return null;
}

/**
* Finds the paths that correspond to a given entity and method and adds the
* corresponding Ddl items to the model
*/
void addDdls(TdEntity entity, String method) {
for (EntityPath entityPath : this.entityPaths.get(entity.getName())) {
if (entityPath.entityName.equalsIgnoreCase(entity.getName()) && entityPath.method.equals(method)) {
for (EntityPath entityPath : this.entityPaths.get(method)) {
if (entityPath.entityName.equalsIgnoreCase(entity.getName())) {
entity.addDdlsItem(new Ddl().command(method).query(entityPath.path));
// note that there could be more than one item for a given entity and method.
}
Expand All @@ -178,8 +111,8 @@ void addDdls(TdEntity entity, String method) {
*/
List<Parameter> getPathParams(String entityName) {
List<Parameter> oaParams = new ArrayList<>();
for (EntityPath entityPath : entityPaths.get(entityName)) {
if (entityPath.entityName.equalsIgnoreCase(entityName) && entityPath.method.equals(POST)) {
for (EntityPath entityPath : entityPaths.get("post")) {
if (entityPath.entityName.equalsIgnoreCase(entityName)) {
for (Parameter oaParam : ModelUtil.safe(entityPath.oaParams)) {
if (oaParam.getIn().equals("path")) {
oaParams.add(oaParam);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import giis.tdrules.client.oa.OaSchemaLogger;
import giis.tdrules.model.OaExtensions;
import giis.tdrules.client.oa.shared.OaSchemaLogger;
import giis.tdrules.client.oa.shared.OaUtil;
import giis.tdrules.model.shared.OaExtensions;
import giis.tdrules.openapi.model.TdAttribute;
import giis.tdrules.openapi.model.TdCheck;
import giis.tdrules.openapi.model.TdEntity;
Expand Down

This file was deleted.

Loading

0 comments on commit ee1e6f0

Please sign in to comment.