Skip to content

Commit 02136e6

Browse files
📝 Add docstrings to validator-json-schema-parser-migration
Docstrings generation was requested by @predic8. * #2225 (comment) The following files were modified: * `core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/ValidatorInterceptor.java` * `core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/json/JSONSchemaVersionParser.java` * `core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/json/JSONYAMLSchemaValidator.java` * `core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/json/MembraneSchemaLoader.java`
1 parent ae65d4b commit 02136e6

4 files changed

Lines changed: 151 additions & 8 deletions

File tree

core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/ValidatorInterceptor.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ public void init() {
9090
throw new ConfigurationException("validator/@skipFaults only makes sense with validator/@wsdl.");
9191
}
9292

93+
/**
94+
* Selects and constructs the appropriate MessageValidator based on configured attributes
95+
* (wsdl, schema, jsonSchema, schematron) or a SOAP proxy's WSDL.
96+
*
97+
* @return the constructed MessageValidator for the selected validation source
98+
* @throws RuntimeException if no validator configuration or SOAP proxy WSDL is available
99+
* @throws Exception if an error occurs while constructing the chosen validator
100+
*/
93101
private MessageValidator getMessageValidator() throws Exception {
94102
if (wsdl != null) {
95103
return new WSDLValidator(resourceResolver, combine(getBaseLocation(), wsdl), serviceName, createFailureHandler(), skipFaults);
@@ -207,28 +215,39 @@ public String getJsonSchema() {
207215
}
208216

209217
/**
210-
* @description The JSON Schema (URL or file) to validate against.
211-
* @example examples/validation/json-schema/schema2000.json
218+
* Specifies the JSON or YAML Schema location used for validation.
219+
*
220+
* @param jsonSchema the schema location — a URL or file system path pointing to a JSON or YAML schema
212221
*/
213222
@MCAttribute
214223
public void setJsonSchema(String jsonSchema) {
215224
this.jsonSchema = jsonSchema;
216225
}
217226

227+
/**
228+
* The schema version identifier used for JSON/YAML schema validation.
229+
*
230+
* @return the configured schema version string (for example, "2020-12")
231+
*/
218232
public String getSchemaVersion() {
219233
return schemaVersion;
220234
}
221235

222236
/**
223-
* @description The version of the Schema.
224-
* @example 04, 05, 06, 07, 2019-09, 2020-12
225-
* @default 2020-12
237+
* Set the JSON/YAML Schema version identifier used when validating JSON/YAML schemas.
238+
*
239+
* @param version the schema version identifier (examples: "04", "05", "06", "07", "2019-09", "2020-12"); defaults to "2020-12"
226240
*/
227241
@MCAttribute
228242
public void setSchemaVersion(String version) {
229243
this.schemaVersion = version;
230244
}
231245

246+
/**
247+
* Returns the configured Schematron schema location used for validation.
248+
*
249+
* @return the Schematron location as a string, or `null` if not configured
250+
*/
232251
public String getSchematron() {
233252
return schematron;
234253
}
@@ -279,6 +298,14 @@ public String getShortDescription() {
279298
return validator.getInvalid() + " of " + (validator.getValid() + validator.getInvalid()) + " messages have been invalid.";
280299
}
281300

301+
/**
302+
* Constructs a descriptive string that lists which validation artifacts were used and provides links to them.
303+
*
304+
* The description starts from the short summary and, when configured, appends references and clickable links
305+
* for the WSDL, XML Schema, JSON Schema, and Schematron resources used for validation.
306+
*
307+
* @return a human-readable description of validation outcomes including links to configured validation resources
308+
*/
282309
@Override
283310
public String getLongDescription() {
284311
StringBuilder sb = new StringBuilder(getShortDescription());
@@ -319,4 +346,4 @@ private FailureHandler createFailureHandler() {
319346
throw new IllegalArgumentException("Unknown failureHandler type: " + failureHandler);
320347
}
321348

322-
}
349+
}

core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/json/JSONSchemaVersionParser.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,28 @@
99

1010
public class JSONSchemaVersionParser {
1111

12+
/**
13+
* Resolve a JSON Schema version alias to its corresponding SpecVersion.VersionFlag.
14+
*
15+
* @param version a version alias such as "04", "draft-04", "07", "draft-2019-09" or "2020-12"
16+
* @return the SpecVersion.VersionFlag that corresponds to the given alias
17+
* @throws com.predic8.membrane.core.util.ConfigurationException if the alias is not a recognized JSON Schema version
18+
*/
1219
public static SpecVersion.VersionFlag parse(String version) {
1320
return SpecVersion.VersionFlag.fromId(aliasToSpecId(version)).get();
1421
}
1522

23+
/**
24+
* Resolve a JSON Schema version alias to the internal SchemaId string.
25+
*
26+
* <p>Recognized aliases include short numeric forms (e.g. "04", "06", "07"),
27+
* draft-prefixed forms (e.g. "draft-04", "draft-07") and calendar-year forms
28+
* (e.g. "2019-09", "2020-12" or "draft-2019-09", "draft-2020-12").</p>
29+
*
30+
* @param alias the user-facing version alias to resolve (e.g. "draft-07", "2019-09")
31+
* @return the corresponding internal SchemaId string (one of V4, V6, V7, V201909, V202012)
32+
* @throws ConfigurationException if the alias is not one of the recognized versions
33+
*/
1634
static String aliasToSpecId(String alias) {
1735
return switch (alias) {
1836
case "04","draft-04" -> V4;
@@ -23,4 +41,4 @@ static String aliasToSpecId(String alias) {
2341
default -> throw new ConfigurationException("Unknown JSON Schema version: " + alias);
2442
};
2543
}
26-
}
44+
}

core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/json/JSONYAMLSchemaValidator.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,50 @@ public class JSONYAMLSchemaValidator extends AbstractMessageValidator {
5959
*/
6060
JsonSchema schema;
6161

62+
/**
63+
* Construct a JSONYAMLSchemaValidator configured with a resolver, the JSON Schema source, a failure handler, and the schema version.
64+
*
65+
* @param resolver resolver used to load referenced schemas and resources
66+
* @param jsonSchema the schema location or JSON/YAML schema content to validate against
67+
* @param failureHandler handler used to build problem-detail responses on validation failures
68+
* @param schemaVersion JSON Schema version identifier (e.g., "2020-12"); parsed into the validator's internal schemaId
69+
*/
6270
public JSONYAMLSchemaValidator(Resolver resolver, String jsonSchema, FailureHandler failureHandler, String schemaVersion) {
6371
this.resolver = resolver;
6472
this.jsonSchema = jsonSchema;
6573
this.failureHandler = failureHandler;
6674
this.schemaId = JSONSchemaVersionParser.parse( schemaVersion);
6775
}
6876

77+
/**
78+
* Creates a JSONYAMLSchemaValidator using the default JSON Schema version "2020-12".
79+
*
80+
* @param resolver resolver used to load external schemas or resources referenced by the JSON Schema
81+
* @param jsonSchema schema location or the schema content to validate messages against
82+
* @param failureHandler handler used to build and set problem-detail responses for validation failures
83+
*/
6984
public JSONYAMLSchemaValidator(Resolver resolver, String jsonSchema, FailureHandler failureHandler) {
7085
this(resolver, jsonSchema, failureHandler, "2020-12");
7186
}
7287

88+
/**
89+
* Provides the validator's display name.
90+
*
91+
* @return the name "JSON Schema Validator"
92+
*/
7393
@Override
7494
public String getName() {
7595
return "JSON Schema Validator";
7696
}
7797

98+
/**
99+
* Initializes the JSON Schema validation infrastructure for this validator.
100+
*
101+
* <p>Configures a thread-safe JsonSchemaFactory (using the configured schema version and the
102+
* instance's Resolver), builds the SchemaValidatorsConfig, loads the JsonSchema from the
103+
* configured schema location, and initializes its validators so the schema instance is ready
104+
* for concurrent validation calls.</p>
105+
*/
78106
@Override
79107
public void init() {
80108
super.init();
@@ -97,10 +125,27 @@ public void init() {
97125

98126
}
99127

128+
/**
129+
* Validates the message body of the given exchange against the configured JSON Schema using UTF-8 encoding.
130+
*
131+
* @param exc the exchange containing the message to validate
132+
* @param flow the flow (REQUEST/RESPONSE) indicating which message direction to validate
133+
* @return an Outcome indicating whether processing should continue (`CONTINUE`) or be aborted (`ABORT`)
134+
*/
100135
public Outcome validateMessage(Exchange exc, Flow flow) throws Exception {
101136
return validateMessage(exc, flow, UTF_8);
102137
}
103138

139+
/**
140+
* Validates the message body for the given flow against the configured JSON Schema and sets a problem-details
141+
* response on the exchange when validation fails.
142+
*
143+
* @param exc the exchange containing the message to validate
144+
* @param flow the flow (e.g. REQUEST or RESPONSE) whose message is validated
145+
* @param ignored unused charset parameter (kept for API compatibility)
146+
* @return `CONTINUE` if the message conforms to the schema, `ABORT` if validation failed and a
147+
* problem-details response was set on the exchange
148+
*/
104149
public Outcome validateMessage(Exchange exc, Flow flow, Charset ignored) throws Exception {
105150

106151
Set<ValidationMessage> assertions = schema.validate(exc.getMessage(flow).getBodyAsStringDecoded(), JSON);
@@ -124,10 +169,31 @@ public Outcome validateMessage(Exchange exc, Flow flow, Charset ignored) throws
124169
return ABORT;
125170
}
126171

172+
/**
173+
* Convert a set of schema validation messages into a list of problem-detail maps.
174+
*
175+
* @param assertions the validation messages produced by JSON Schema validation
176+
* @return a list of maps where each map contains problem-detail fields derived from a validation message
177+
*/
127178
private @NotNull List<Map<String, Object>> getMapForProblemDetails(Set<ValidationMessage> assertions) {
128179
return assertions.stream().map(this::validationMessageToProblemDetailsMap).toList();
129180
}
130181

182+
/**
183+
* Converts a ValidationMessage into an ordered map of problem-detail fields suitable for inclusion
184+
* in a Problem Details response.
185+
*
186+
* @param vm the ValidationMessage to convert
187+
* @return a LinkedHashMap with the following keys:
188+
* "message" (human-readable message),
189+
* "code" (validation code),
190+
* "key" (message key),
191+
* "details" (optional additional details),
192+
* "type" (message type),
193+
* "error" (error identifier),
194+
* "pointer" (RFC 6901 JSON Pointer to the failing location),
195+
* "node" (the JSON node instance related to the message)
196+
*/
131197
private @NotNull Map<String, Object> validationMessageToProblemDetailsMap(ValidationMessage vm) {
132198
Map<String, Object> m = new LinkedHashMap<>();
133199
m.put("message", vm.getMessage());
@@ -142,6 +208,12 @@ public Outcome validateMessage(Exchange exc, Flow flow, Charset ignored) throws
142208
return m;
143209
}
144210

211+
/**
212+
* Builds an RFC 6901 JSON Pointer string for the given evaluation path.
213+
*
214+
* @param evaluationPath the JSON node path to convert; may be null or empty
215+
* @return the JSON Pointer string (empty string if the path is null or has no components)
216+
*/
145217
private String getPointer(JsonNodePath evaluationPath) {
146218
if (evaluationPath == null || evaluationPath.getNameCount() == 0) {
147219
return "";
@@ -160,16 +232,31 @@ private String getPointer(JsonNodePath evaluationPath) {
160232
return sb.toString();
161233
}
162234

235+
/**
236+
* Provides the count of successfully validated messages.
237+
*
238+
* @return the number of messages that passed validation
239+
*/
163240
@Override
164241
public long getValid() {
165242
return valid.get();
166243
}
167244

245+
/**
246+
* Current count of messages that failed JSON Schema validation.
247+
*
248+
* @return the number of messages that failed validation
249+
*/
168250
@Override
169251
public long getInvalid() {
170252
return invalid.get();
171253
}
172254

255+
/**
256+
* Provides the human-readable title used for JSON validation error responses.
257+
*
258+
* @return the error title "JSON validation failed"
259+
*/
173260
@Override
174261
public String getErrorTitle() {
175262
return "JSON validation failed";

core/src/main/java/com/predic8/membrane/core/interceptor/schemavalidation/json/MembraneSchemaLoader.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,21 @@ public class MembraneSchemaLoader implements SchemaLoader {
2222

2323
Resolver resolver;
2424

25+
/**
26+
* Create a MembraneSchemaLoader that uses the given Resolver to locate schema resources.
27+
*
28+
* @param resolver the Resolver used to resolve schema IRIs to input streams containing schema content
29+
*/
2530
public MembraneSchemaLoader(Resolver resolver) {
2631
this.resolver = resolver;
2732
}
2833

34+
/**
35+
* Create an InputStreamSource for the schema identified by the provided absolute IRI.
36+
*
37+
* @param absoluteIri the absolute IRI of the JSON schema to load
38+
* @return an InputStreamSource that provides an InputStream for the schema content resolved from the given IRI
39+
*/
2940
@Override
3041
public InputStreamSource getSchema(AbsoluteIri absoluteIri) {
3142

@@ -37,4 +48,4 @@ public InputStreamSource getSchema(AbsoluteIri absoluteIri) {
3748

3849

3950
}
40-
}
51+
}

0 commit comments

Comments
 (0)