|
6 | 6 | package io.openapiparser; |
7 | 7 |
|
8 | 8 | import io.openapiparser.model.v31.OpenApi; |
9 | | -import io.openapiprocessor.jsonschema.schema.Bucket; |
10 | | -import io.openapiprocessor.jsonschema.schema.DocumentStore; |
11 | | -import io.openapiprocessor.jsonschema.schema.SchemaStore; |
| 9 | +import io.openapiprocessor.jsonschema.ouput.OutputConverter; |
| 10 | +import io.openapiprocessor.jsonschema.ouput.OutputUnit; |
| 11 | +import io.openapiprocessor.jsonschema.schema.*; |
12 | 12 | import io.openapiprocessor.jsonschema.validator.Validator; |
| 13 | +import io.openapiprocessor.jsonschema.validator.steps.ValidationStep; |
13 | 14 |
|
14 | 15 | import java.util.Collection; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +import static io.openapiparser.OpenApiSchemas.*; |
15 | 20 |
|
16 | 21 | public class OpenApiResult31 implements OpenApiResult { |
17 | 22 | @Deprecated |
@@ -53,19 +58,55 @@ public <T> T getModel (Class<T> api) { |
53 | 58 | } |
54 | 59 |
|
55 | 60 | @Override |
56 | | - public boolean validate (Validator validator, SchemaStore schemaStore) { |
57 | | - // not yet supported... |
58 | | - |
59 | | -// JsonSchema schema = schemaStore.addSchema (OPENAPI_SCHEMA_31_ID, OPENAPI_SCHEMA_31); |
60 | | -// JsonInstance instance = new JsonInstance (root.getRawValues (), context.getInstanceContext ()); |
61 | | -// ValidationStep result = validator.validate (schema, instance); |
62 | | -// validationMessages = result.getMessages (); |
63 | | -// return validationMessages.isEmpty (); |
64 | | - return true; |
| 61 | + public Collection<ValidationError> getValidationErrors () { |
| 62 | + return validationErrors; |
65 | 63 | } |
66 | 64 |
|
67 | 65 | @Override |
68 | | - public Collection<ValidationError> getValidationErrors () { |
69 | | - return validationErrors; |
| 66 | + public boolean validate (Validator validator, SchemaStore schemaStore) { |
| 67 | + try { |
| 68 | + schemaStore.register (OPENAPI_SCHEMA_31_ID, OPENAPI_SCHEMA_31); |
| 69 | + JsonSchema schema = schemaStore.getSchema (OPENAPI_SCHEMA_31_ID, SchemaVersion.Draft202012); |
| 70 | + |
| 71 | + Object bundle = bundle (); |
| 72 | + JsonInstance instance = new JsonInstance (bundle); |
| 73 | + ValidationStep result = validator.validate (schema, instance); |
| 74 | + |
| 75 | + OutputConverter converter = new OutputConverter (Output.BASIC); |
| 76 | + OutputUnit output = converter.convert (result); |
| 77 | + |
| 78 | + if (output.isValid ()) { |
| 79 | + validationErrors = Collections.emptyList (); |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + Collection<OutputUnit> errors = output.getErrors (); |
| 84 | + assert errors != null; |
| 85 | + |
| 86 | + validationErrors = errors |
| 87 | + .stream () |
| 88 | + .map (e -> { |
| 89 | + return new ValidationError ( |
| 90 | + e.getInstanceLocation (), |
| 91 | + e.getKeywordLocation (), |
| 92 | + e.getAbsoluteKeywordLocation (), |
| 93 | + e.getError () |
| 94 | + ); |
| 95 | + }) |
| 96 | + .collect (Collectors.toList ()); |
| 97 | + |
| 98 | + for (OutputUnit error : errors) { |
| 99 | + error.getError (); |
| 100 | + error.getInstanceLocation (); |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } catch (Exception ex) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + Object bundle () { |
| 110 | + return new OpenApiBundler (context, documents, root).bundle (); |
70 | 111 | } |
71 | 112 | } |
0 commit comments