Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
75abf92
6.x json2xml array support (#2364)
christiangoerdes Dec 4, 2025
642c0c5
Restrict JAVA_OPTS path normalization to log4j.configurationFile in s…
christiangoerdes Dec 5, 2025
28968c1
fix javadoc
christiangoerdes Dec 5, 2025
2c0f3e2
Merge remote-tracking branch 'origin/6.X' into 6.X
christiangoerdes Dec 5, 2025
4ba8b06
fix javadoc
christiangoerdes Dec 5, 2025
1a32a4c
Release 6.3.11 (#2384)
github-actions[bot] Dec 5, 2025
3beaa7e
Snapshot version (#2385)
github-actions[bot] Dec 5, 2025
0757b57
Add support for reference schemas in JSON/YAML schema validation (inf…
christiangoerdes Dec 18, 2025
d164acc
Make `schemas` field private in ReferenceSchemas
christiangoerdes Dec 18, 2025
305a08c
Improve logging for unsupported referenceSchemas in schema validation
christiangoerdes Dec 18, 2025
ab8d0f7
Refactor schema validation logging and add example for JSON schema wi…
christiangoerdes Dec 19, 2025
5ad0344
Add tests for JSON schema validation with reference mappings
christiangoerdes Dec 19, 2025
51ba494
improvements
christiangoerdes Dec 19, 2025
a59fac2
Merge branch 'master' into JsonSchema-Schema-mapping
christiangoerdes Dec 19, 2025
e8aa347
Refactor `JSONYAMLSchemaValidator` to use updated schema validation A…
christiangoerdes Dec 19, 2025
3974f47
Add license headers and null check for schemaMappings in ValidatorInt…
christiangoerdes Dec 19, 2025
d174a59
Merge branch 'master' into JsonSchema-Schema-mapping
predic8 Dec 20, 2025
42f1a90
refactor: simplify JSON Schema validation setup and update factorypat…
predic8 Dec 20, 2025
577e605
Merge remote-tracking branch 'origin/JsonSchema-Schema-mapping' into …
predic8 Dec 20, 2025
8c95632
refactor: replace JsonSchema factory logic with schema registry and e…
predic8 Dec 20, 2025
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/* Copyright 2025 predic8 GmbH, www.predic8.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.predic8.membrane.annot.generator;

public enum Scope {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/* Copyright 2025 predic8 GmbH, www.predic8.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.predic8.membrane.annot.util;

public class ReflectionUtil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/* Copyright 2025 predic8 GmbH, www.predic8.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.predic8.membrane.annot.util;

import org.junit.jupiter.api.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class ValidatorInterceptor extends AbstractInterceptor implements Applica
private ResolverMap resourceResolver;
private ApplicationContext applicationContext;

private SchemaMappings schemaMappings;

private SOAPProxy soapProxy;

public ValidatorInterceptor() {
Expand Down Expand Up @@ -94,15 +96,23 @@ public void init() {

private MessageValidator getMessageValidator() throws Exception {
if (wsdl != null) {
if (schemaMappings != null)
logIgnoringRefSchemas();
return new WSDLValidator(resourceResolver, combine(getBaseLocation(), wsdl), serviceName, createFailureHandler(), skipFaults);
}
if (schema != null) {
if (schemaMappings != null)
logIgnoringRefSchemas();
return new XMLSchemaValidator(resourceResolver, combine(getBaseLocation(), schema), createFailureHandler());
}
if (jsonSchema != null) {
return new JSONYAMLSchemaValidator(resourceResolver, combine(getBaseLocation(), jsonSchema), createFailureHandler(), schemaVersion);
return new JSONYAMLSchemaValidator(resourceResolver, combine(getBaseLocation(), jsonSchema), createFailureHandler(), schemaVersion) {{
if(schemaMappings != null) setSchemaMappings(schemaMappings.getSchemaMap());
}};
}
Comment thread
christiangoerdes marked this conversation as resolved.
if (schematron != null) {
if (schemaMappings != null)
logIgnoringRefSchemas();
return new SchematronValidator(combine(getBaseLocation(), schematron), createFailureHandler(), router, applicationContext);
}

Expand All @@ -112,6 +122,10 @@ private MessageValidator getMessageValidator() throws Exception {
throw new RuntimeException("Validator is not configured properly. <validator> must have an attribute specifying the validator.");
}

private static void logIgnoringRefSchemas() {
log.warn("Ignoring 'referenceSchemas': schema references are only supported for JSON/YAML validators");
}

private @Nullable WSDLValidator getWsdlValidatorFromSOAPProxy() {
if(soapProxy == null) return null;
wsdl = soapProxy.getWsdl();
Expand Down Expand Up @@ -319,6 +333,16 @@ private FailureHandler createFailureHandler() {
throw new IllegalArgumentException("Unknown failureHandler type: " + failureHandler);
}

@MCChildElement
public void setReferenceSchemas(SchemaMappings schemaMappings) {
this.schemaMappings = schemaMappings;
}

public SchemaMappings getReferenceSchemas() {
return schemaMappings;
}


public void setSoapProxy(SOAPProxy soapProxy) {
this.soapProxy = soapProxy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.networknt.schema.*;
import com.networknt.schema.Error;
import com.networknt.schema.path.NodePath;
import com.networknt.schema.resource.SchemaLoader;
import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.interceptor.Interceptor.*;
import com.predic8.membrane.core.interceptor.*;
Expand Down Expand Up @@ -48,7 +49,6 @@ public class JSONYAMLSchemaValidator extends AbstractMessageValidator {

private final YAMLFactory factory = YAMLFactory.builder().enable(STRICT_DUPLICATE_DETECTION).build();
private final ObjectMapper yamlObjectMapper = new ObjectMapper(factory);
private final ObjectMapper jsonObjectMapper = new ObjectMapper();

public static final String SCHEMA_VERSION_2020_12 = "2020-12";

Expand All @@ -60,10 +60,7 @@ public class JSONYAMLSchemaValidator extends AbstractMessageValidator {
private final AtomicLong invalid = new AtomicLong();
private final SpecificationVersion schemaId;

/**
* JsonSchemaFactory instances are thread-safe provided its configuration is not modified.
*/
SchemaRegistry jsonSchemaFactory;
private Map<String, String> schemaMappings = new HashMap<>();

/**
* JsonSchema instances are thread-safe provided its configuration is not modified.
Expand All @@ -76,7 +73,7 @@ public JSONYAMLSchemaValidator(Resolver resolver, String jsonSchema, FailureHand
this.resolver = resolver;
this.jsonSchema = jsonSchema;
this.failureHandler = failureHandler;
this.schemaId = JSONSchemaVersionParser.parse( schemaVersion);
this.schemaId = JSONSchemaVersionParser.parse(schemaVersion);
this.inputFormat = inputFormat;
}

Expand All @@ -97,26 +94,34 @@ public String getName() {
public void init() {
super.init();

jsonSchemaFactory = SchemaRegistry.withDefaultDialect(schemaId, builder ->
builder.schemaLoader(loaders -> new MembraneSchemaLoader(resolver)));

try (InputStream in = resolver.resolve(jsonSchema)) {
schema = jsonSchemaFactory.getSchema((jsonSchema.endsWith(".yaml") || jsonSchema.endsWith(".yml") ? yamlObjectMapper: jsonObjectMapper).readTree(in));
schema = createSchemaRegistry().getSchema(SchemaLocation.of(jsonSchema), in, getSchemaFormat());
schema.initializeValidators();
} catch (IOException e) {
throw new RuntimeException("Cannot read JSON Schema from: " + jsonSchema, e);
}
}

private @NotNull InputFormat getSchemaFormat() {
return (jsonSchema.toLowerCase().endsWith(".yaml") || jsonSchema.toLowerCase().endsWith(".yml")) ? YAML : JSON;
}

private SchemaRegistry createSchemaRegistry() {
return SchemaRegistry.withDefaultDialect(schemaId, b -> b.schemaLoader(SchemaLoader.builder()
.schemaIdResolvers(r -> r.mappings(schemaMappings))
.resourceLoaders(rl -> rl.values(list -> list.addFirst(new MembraneSchemaLoader(resolver))))
.build()));
}

public Outcome validateMessage(Exchange exc, Flow flow) throws Exception {
return validateMessage(exc, flow, UTF_8);
}

public Outcome validateMessage(Exchange exc, Flow flow, Charset ignored) throws Exception {

List<Error> assertions = inputFormat == YAML ?
handleMultipleYAMLDocuments(exc, flow) :
schema.validate(exc.getMessage(flow).getBodyAsStringDecoded(), inputFormat);
handleMultipleYAMLDocuments(exc, flow) :
schema.validate(exc.getMessage(flow).getBodyAsStringDecoded(), inputFormat);

if (assertions.isEmpty()) {
valid.incrementAndGet();
Expand Down Expand Up @@ -204,4 +209,8 @@ public long getInvalid() {
public String getErrorTitle() {
return "JSON validation failed";
}

public void setSchemaMappings(Map<String, String> schemaMappings) {
this.schemaMappings = schemaMappings;
}
Comment thread
christiangoerdes marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Copyright 2025 predic8 GmbH, www.predic8.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.predic8.membrane.core.interceptor.schemavalidation.json;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.annot.Required;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@MCElement(name = "schemaMappings")
public class SchemaMappings {

private List<Schema> schemas = new ArrayList<>();

public Map<String, String> getSchemaMap() {
Map<String, String > referenceSchemas = new HashMap<>();
schemas.forEach(schema -> referenceSchemas.put(schema.getId(), schema.getLocation()));
return referenceSchemas;
}
Comment thread
christiangoerdes marked this conversation as resolved.

@Required
@MCChildElement
public void setSchemas(List<Schema> schemas) {
this.schemas = schemas;
}

public List<Schema> getSchemas() {
return schemas;
}

@MCElement(name = "schema")
public static class Schema {
private String id;

private String location;

@MCAttribute
public void setId(String id) {
this.id = id;
}

public String getId() {
return id;
}

@MCAttribute
public void setLocation(String location) {
this.location = location;
}

public String getLocation() {
return location;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Validation - JSON Schema with Schema Mappings

This sample explains how to set up and use the `validator` plugin with JSON Schemas that reference external schemas
by `$ref` URN/ID, and how to map those references to local files via `schemaMappings`.

## Running the Example

1. Go to the directory:
`<membrane-root>/examples/validation/json-schema/schema-mappings`

2. Start `membrane.cmd` or `membrane.sh`.

3. Look at `schemas/schema2000.json` and note the `$ref` to `urn:app:base_parameter_def`. Then open `schemas/base-param.json` and compare the schema to `good2000.json` and `bad2000.json`.

4. Run `curl -H "Content-Type: application/json" -d @good2000.json http://localhost:2000/` on the console. Observe that you get a successful response.

5. Run `curl -H "Content-Type: application/json" -d @bad2000.json http://localhost:2000/`. Observe that you get a validation error response.

Keeping the router running, you can try a more complex setup with multiple referenced schemas.

1. Have a look at `schemas/schema2001.json` and note the `$ref`s to `urn:app:base_parameter_def` and `urn:app:meta_def`. Then open `schemas/base-param.json` and `schemas/meta.json` and compare the schemas to `good2001.json` and `bad2001.json`.

2. Run `curl -H "Content-Type: application/json" -d @good2001.json http://localhost:2001/`. Observe that you get a successful response.

3. Run `curl -H "Content-Type: application/json" -d @bad2001.json http://localhost:2001/`. Observe that you get a validation error response.

## How it is done

In `proxies.xml`, each API configures a `<validator jsonSchema="...">`.
The root schemas contain `$ref` references to URN/IDs, for example:

- `urn:app:base_parameter_def#/$defs/BaseParameter`
- `urn:app:meta_def#/$defs/Meta`

To let Membrane resolve these URNs, you map them in the validator using:

```xml
<schemaMappings>
<schema id="urn:app:base_parameter_def" location="schemas/base-parameter.json"/>
<schema id="urn:app:meta_def" location="schemas/meta.json"/>
</schemaMappings>
Comment thread
christiangoerdes marked this conversation as resolved.
````

Only if validation succeeds, the request is forwarded to the backend (port 2002).

---

See:

* [JSON Schema](https://json-schema.org/) documentation
* [validator](https://www.membrane-api.io/docs/current/validator.html) reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"param": {
"name": "limit"
},
"timestamp": "not-a-date",
"extra": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"params": [
{
"name": "mode",
"value": "fast",
"unexpected": "foo"
}
],
"meta": {
"source": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"param": {
"name": "limit",
"value": 100
},
"timestamp": "2025-12-19T10:15:30Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"params": [
{
"name": "mode",
"value": "fast"
},
{
"name": "retries",
"value": 3
}
],
"meta": {
"source": "curl",
"requestId": "REQ-12345678"
}
}
Loading
Loading