-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Updated Form-JS key input to be a dropdown of path options #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
builder-api/src/main/java/org/acme/constants/CheckStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
builder-api/src/main/java/org/acme/enums/EvaluationResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| package org.acme.enums; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public enum EvaluationResult { | ||
| TRUE("TRUE"), | ||
| FALSE("FALSE"), | ||
|
|
||
26 changes: 26 additions & 0 deletions
26
builder-api/src/main/java/org/acme/model/dto/FormPathsResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.acme.model.dto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Response DTO for the form paths endpoint. | ||
| * Contains the list of unique input paths required by all checks in a screener. | ||
| */ | ||
| public class FormPathsResponse { | ||
| private List<String> paths; | ||
|
|
||
| public FormPathsResponse() { | ||
| } | ||
|
|
||
| public FormPathsResponse(List<String> paths) { | ||
| this.paths = paths; | ||
| } | ||
|
|
||
| public List<String> getPaths() { | ||
| return paths; | ||
| } | ||
|
|
||
| public void setPaths(List<String> paths) { | ||
| this.paths = paths; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
builder-api/src/main/java/org/acme/service/FormDataTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package org.acme.service; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Utility class for transforming form data between different formats. | ||
| * | ||
| * The Form-JS editor uses a "people" object with personId keys (e.g., {applicant: {...}, spouse: {...}}), | ||
| * while DMN models expect a "people" array with id fields (e.g., [{id: "applicant", ...}, {id: "spouse", ...}]). | ||
| */ | ||
| public class FormDataTransformer { | ||
|
|
||
| /** | ||
| * Transforms form data by converting a "people" object (with personId keys) into a "people" array | ||
| * (with id fields). This is the reverse of the frontend's transformInputDefinitionSchema function. | ||
| * | ||
| * @param formData The form data from the user, potentially containing a "people" object | ||
| * @return A new Map with the "people" object converted to an array, or the original data if no transformation needed | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static Map<String, Object> transformFormData(Map<String, Object> formData) { | ||
| if (formData == null) { | ||
| return new HashMap<>(); | ||
| } | ||
|
|
||
| Object peopleValue = formData.get("people"); | ||
|
|
||
| // If no people key, or it's already a List (array), return a copy of the original | ||
| if (peopleValue == null || peopleValue instanceof List) { | ||
| return new HashMap<>(formData); | ||
| } | ||
|
|
||
| // If people is not a Map (object), return a copy of the original | ||
| if (!(peopleValue instanceof Map)) { | ||
| return new HashMap<>(formData); | ||
| } | ||
|
|
||
| Map<String, Object> peopleObject = (Map<String, Object>) peopleValue; | ||
| List<Map<String, Object>> peopleArray = new ArrayList<>(); | ||
|
|
||
| // Convert each entry in the people object to an array element with an "id" field | ||
| for (Map.Entry<String, Object> entry : peopleObject.entrySet()) { | ||
| String personId = entry.getKey(); | ||
| Object personValue = entry.getValue(); | ||
|
|
||
| if (personValue instanceof Map) { | ||
| Map<String, Object> personData = new HashMap<>((Map<String, Object>) personValue); | ||
| personData.put("id", personId); | ||
| peopleArray.add(personData); | ||
| } else { | ||
| // If the value is not a Map, create a simple object with just the id | ||
| Map<String, Object> personData = new HashMap<>(); | ||
| personData.put("id", personId); | ||
| peopleArray.add(personData); | ||
| } | ||
| } | ||
|
|
||
| // Create the result with the transformed people array | ||
| Map<String, Object> result = new HashMap<>(formData); | ||
| result.put("people", peopleArray); | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
170 changes: 170 additions & 0 deletions
170
builder-api/src/main/java/org/acme/service/InputSchemaService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package org.acme.service; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
|
|
||
| import org.acme.model.domain.Benefit; | ||
| import org.acme.model.domain.CheckConfig; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| /** | ||
| * Service for transforming and extracting paths from JSON Schema input definitions. | ||
| */ | ||
| @ApplicationScoped | ||
| public class InputSchemaService { | ||
|
|
||
| private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| /** | ||
| * Extracts all unique input paths from all benefits in a screener. | ||
| * | ||
| * @param benefits List of benefits containing checks with inputDefinitions | ||
| * @return Set of unique dot-separated paths (e.g., "people.applicant.dateOfBirth") | ||
| */ | ||
| public Set<String> extractAllInputPaths(List<Benefit> benefits) { | ||
| Set<String> pathSet = new HashSet<>(); | ||
|
|
||
| for (Benefit benefit : benefits) { | ||
| List<CheckConfig> checks = benefit.getChecks(); | ||
| if (checks == null) continue; | ||
|
|
||
| for (CheckConfig check : checks) { | ||
| JsonNode transformedSchema = transformInputDefinitionSchema(check); | ||
| List<String> paths = extractJsonSchemaPaths(transformedSchema); | ||
| pathSet.addAll(paths); | ||
| } | ||
| } | ||
|
|
||
| return pathSet; | ||
| } | ||
|
|
||
| /** | ||
| * Transforms a CheckConfig's inputDefinition JSON Schema by converting the `people` | ||
| * array property into an object with personId-keyed properties nested under it. | ||
| * | ||
| * Example: | ||
| * Input: { people: { type: "array", items: { properties: { dateOfBirth: ... } } } } | ||
| * Output: { people: { type: "object", properties: { [personId]: { properties: { dateOfBirth: ... } } } } } | ||
| * | ||
| * @param checkConfig The CheckConfig containing inputDefinition and parameters | ||
| * @return A new JsonNode with `people` transformed to an object with personId-keyed properties | ||
| */ | ||
| public JsonNode transformInputDefinitionSchema(CheckConfig checkConfig) { | ||
| JsonNode inputDefinition = checkConfig.getInputDefinition(); | ||
|
|
||
| if (inputDefinition == null || !inputDefinition.has("properties")) { | ||
| return inputDefinition != null ? inputDefinition.deepCopy() : objectMapper.createObjectNode(); | ||
| } | ||
|
|
||
| JsonNode properties = inputDefinition.get("properties"); | ||
| JsonNode peopleProperty = properties.get("people"); | ||
| boolean hasPeopleProperty = peopleProperty != null; | ||
|
|
||
| // Extract personId from parameters | ||
| Map<String, Object> parameters = checkConfig.getParameters(); | ||
| String personId = parameters != null ? (String) parameters.get("personId") : null; | ||
|
|
||
| // If people property exists but no personId, return original (can't transform) | ||
| if (hasPeopleProperty && (personId == null || personId.isEmpty())) { | ||
| return inputDefinition.deepCopy(); | ||
| } | ||
|
|
||
| // If no people property, return a copy of the original schema | ||
| if (!hasPeopleProperty) { | ||
| return inputDefinition.deepCopy(); | ||
| } | ||
|
|
||
| // Deep clone the schema to avoid mutations | ||
| ObjectNode transformedSchema = inputDefinition.deepCopy(); | ||
| ObjectNode transformedProperties = (ObjectNode) transformedSchema.get("properties"); | ||
|
|
||
| // Get the items schema from the people array | ||
| JsonNode itemsSchema = peopleProperty.get("items"); | ||
|
|
||
| // Transform people from array to object with personId as a nested property | ||
| ObjectNode newPeopleSchema = objectMapper.createObjectNode(); | ||
| newPeopleSchema.put("type", "object"); | ||
| ObjectNode newPeopleProperties = objectMapper.createObjectNode(); | ||
| if (itemsSchema != null) { | ||
| newPeopleProperties.set(personId, itemsSchema.deepCopy()); | ||
| } | ||
|
|
||
| newPeopleSchema.set("properties", newPeopleProperties); | ||
| transformedProperties.set("people", newPeopleSchema); | ||
| return transformedSchema; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts all property paths from a JSON Schema inputDefinition. | ||
| * Recursively traverses nested objects to build dot-separated paths. | ||
| * Excludes the top-level "parameters" property and "id" properties. | ||
| * | ||
| * @param jsonSchema The JSON Schema to parse | ||
| * @return List of dot-separated paths (e.g., ["people.applicant.dateOfBirth", "income"]) | ||
| */ | ||
| public List<String> extractJsonSchemaPaths(JsonNode jsonSchema) { | ||
| if (jsonSchema == null || !jsonSchema.has("properties")) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| return traverseSchema(jsonSchema, ""); | ||
| } | ||
|
|
||
| private List<String> traverseSchema(JsonNode schema, String parentPath) { | ||
| List<String> paths = new ArrayList<>(); | ||
|
|
||
| if (schema == null || !schema.has("properties")) { | ||
| return paths; | ||
| } | ||
|
|
||
| JsonNode propertiesJsonNode = schema.get("properties"); | ||
| Iterator<Map.Entry<String, JsonNode>> nestedProperties = propertiesJsonNode.properties().iterator(); | ||
|
|
||
| while (nestedProperties.hasNext()) { | ||
| Map.Entry<String, JsonNode> currentProperty = nestedProperties.next(); | ||
| String propKey = currentProperty.getKey(); | ||
| JsonNode propValue = currentProperty.getValue(); | ||
|
|
||
| // Skip top-level "parameters" property | ||
| if (parentPath.isEmpty() && "parameters".equals(propKey)) { | ||
| continue; | ||
| } | ||
|
|
||
| // Skip "id" properties | ||
| if ("id".equals(propKey)) { | ||
| continue; | ||
| } | ||
|
|
||
| String currentPath = parentPath.isEmpty() ? propKey : parentPath + "." + propKey; | ||
|
|
||
| // If this property has nested properties, recurse into it | ||
| if (propValue.has("properties")) { | ||
| paths.addAll(traverseSchema(propValue, currentPath)); | ||
| } else if ("array".equals(getType(propValue)) && propValue.has("items")) { | ||
| // Handle arrays - recurse into items schema with the current path | ||
| JsonNode itemsSchema = propValue.get("items"); | ||
| if (itemsSchema.has("properties")) { | ||
| paths.addAll(traverseSchema(itemsSchema, currentPath)); | ||
| } else { | ||
| // Array of primitives - add the path | ||
| paths.add(currentPath); | ||
| } | ||
| } else { | ||
| // Leaf property - add the path | ||
| paths.add(currentPath); | ||
| } | ||
| } | ||
|
|
||
| return paths; | ||
| } | ||
|
|
||
| private String getType(JsonNode schema) { | ||
| if (schema == null || !schema.has("type")) { | ||
| return null; | ||
| } | ||
| return schema.get("type").asText(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be over-engineering to do this now. But if we add additional transformations, I think it would be great if this function iterated through a list of transformations. Or somehow setup up so that we could easily add new transformations while changing as little code as possible.