Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions src/main/java/utils/MappingUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,6 @@ public static JsonSchema setIdNull(JsonSchema schema) {
return schema;
}

/**
* Generates a JSON schema for the given class, with the option to specify required fields.
* This method uses the Jackson library to generate the schema and customize it based on the provided required fields.
* It sets the ID of the schema and its nested schemas to null and adds the required fields to the schema's "required" property.
*
* @param clazz The class for which the JSON schema should be generated.
* @param requiredFields A varargs array of field names that should be marked as "required" in the schema.
* @return A JsonNode representing the generated schema, or null if an exception occurs during generation.
*/
public static JsonNode getJsonNodeFor(Class<?> clazz, String... requiredFields) {
JsonSchema schema = generateSchema(clazz);
assert schema != null;
schema.setId(null);
return addRequiredFields(schema, requiredFields);
}

/**
* Generates a JSON schema for the given class, with the option to specify required fields.
* This method uses the Jackson library to generate the schema.
Expand Down Expand Up @@ -156,32 +140,6 @@ public static JsonSchema generateSchema(Class<?> clazz) {
return null;
}
}

/**
* Adds the specified required fields to the "required" property of a JSON schema.
* This method adds each field name from the provided array to the "required" property of the schema.
*
* @param schema The JSON schema to modify.
* @param requiredFields An array of field names that should be marked as required in the schema.
* @return
*/
private static JsonNode addRequiredFields(JsonSchema schema, String[] requiredFields) {
JsonNode schemaNode = mapper.valueToTree(schema);

if (!(schemaNode instanceof ObjectNode root)) {
throw new IllegalArgumentException("Schema must be an ObjectNode");
}

ArrayNode required = root.withArray("required");

for (String fieldName : requiredFields) {
required.add(fieldName);
}

// Convert the modified JsonNode back to JsonSchema
return mapper.valueToTree(root);
}

}
}
}
Expand Down