-
Notifications
You must be signed in to change notification settings - Fork 167
Add support for reference schemas in JSON/YAML schema validation (7.X) #2477
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
Show all changes
20 commits
Select commit
Hold shift + click to select a range
75abf92
6.x json2xml array support (#2364)
christiangoerdes 642c0c5
Restrict JAVA_OPTS path normalization to log4j.configurationFile in s…
christiangoerdes 28968c1
fix javadoc
christiangoerdes 2c0f3e2
Merge remote-tracking branch 'origin/6.X' into 6.X
christiangoerdes 4ba8b06
fix javadoc
christiangoerdes 1a32a4c
Release 6.3.11 (#2384)
github-actions[bot] 3beaa7e
Snapshot version (#2385)
github-actions[bot] 0757b57
Add support for reference schemas in JSON/YAML schema validation (inf…
christiangoerdes d164acc
Make `schemas` field private in ReferenceSchemas
christiangoerdes 305a08c
Improve logging for unsupported referenceSchemas in schema validation
christiangoerdes ab8d0f7
Refactor schema validation logging and add example for JSON schema wi…
christiangoerdes 5ad0344
Add tests for JSON schema validation with reference mappings
christiangoerdes 51ba494
improvements
christiangoerdes a59fac2
Merge branch 'master' into JsonSchema-Schema-mapping
christiangoerdes e8aa347
Refactor `JSONYAMLSchemaValidator` to use updated schema validation A…
christiangoerdes 3974f47
Add license headers and null check for schemaMappings in ValidatorInt…
christiangoerdes d174a59
Merge branch 'master' into JsonSchema-Schema-mapping
predic8 42f1a90
refactor: simplify JSON Schema validation setup and update factorypat…
predic8 577e605
Merge remote-tracking branch 'origin/JsonSchema-Schema-mapping' into …
predic8 8c95632
refactor: replace JsonSchema factory logic with schema registry and e…
predic8 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
14 changes: 14 additions & 0 deletions
14
annot/src/main/java/com/predic8/membrane/annot/generator/Scope.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
14 changes: 14 additions & 0 deletions
14
annot/src/main/java/com/predic8/membrane/annot/util/ReflectionUtil.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
14 changes: 14 additions & 0 deletions
14
annot/src/test/java/com/predic8/membrane/annot/util/ReflectionUtilTest.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
72 changes: 72 additions & 0 deletions
72
...main/java/com/predic8/membrane/core/interceptor/schemavalidation/json/SchemaMappings.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,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; | ||
| } | ||
|
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; | ||
| } | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
distribution/examples/validation/json-schema/schema-mappings/README.md
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,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> | ||
|
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 | ||
7 changes: 7 additions & 0 deletions
7
distribution/examples/validation/json-schema/schema-mappings/bad2000.json
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,7 @@ | ||
| { | ||
| "param": { | ||
| "name": "limit" | ||
| }, | ||
| "timestamp": "not-a-date", | ||
| "extra": true | ||
| } |
12 changes: 12 additions & 0 deletions
12
distribution/examples/validation/json-schema/schema-mappings/bad2001.json
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,12 @@ | ||
| { | ||
| "params": [ | ||
| { | ||
| "name": "mode", | ||
| "value": "fast", | ||
| "unexpected": "foo" | ||
| } | ||
| ], | ||
| "meta": { | ||
| "source": "" | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
distribution/examples/validation/json-schema/schema-mappings/good2000.json
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,7 @@ | ||
| { | ||
| "param": { | ||
| "name": "limit", | ||
| "value": 100 | ||
| }, | ||
| "timestamp": "2025-12-19T10:15:30Z" | ||
| } |
16 changes: 16 additions & 0 deletions
16
distribution/examples/validation/json-schema/schema-mappings/good2001.json
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,16 @@ | ||
| { | ||
| "params": [ | ||
| { | ||
| "name": "mode", | ||
| "value": "fast" | ||
| }, | ||
| { | ||
| "name": "retries", | ||
| "value": 3 | ||
| } | ||
| ], | ||
| "meta": { | ||
| "source": "curl", | ||
| "requestId": "REQ-12345678" | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.