Skip to content

Commit a2fd275

Browse files
gracekarinafrantuma
authored andcommitted
oas 3.1 - implementation of Examples, unevaluatedProperties, and others JsonSchema fields
1 parent 6628f83 commit a2fd275

File tree

2 files changed

+74
-48
lines changed

2 files changed

+74
-48
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,22 +3115,6 @@ public Map<String, Example> getExamples(ObjectNode obj, String location, ParseRe
31153115
return examples;
31163116
}
31173117

3118-
public List<Example> getExampleList(ArrayNode obj, String location, ParseResult result) {
3119-
List<Example> examples = new ArrayList<>();
3120-
if (obj == null) {
3121-
return examples;
3122-
}
3123-
for (JsonNode item : obj) {
3124-
if (item.getNodeType().equals(JsonNodeType.OBJECT)) {
3125-
Example example = getExample((ObjectNode) item, location, result);
3126-
if (example != null) {
3127-
examples.add(example);
3128-
}
3129-
}
3130-
}
3131-
return examples;
3132-
}
3133-
31343118
public Example getExample(ObjectNode node, String location, ParseResult result) {
31353119
if (node == null)
31363120
return null;
@@ -3347,7 +3331,6 @@ public List<String> getTagsStrings(ArrayNode nodes, String location, ParseResult
33473331
return tags;
33483332
}
33493333

3350-
33513334
public Operation getOperation(ObjectNode obj, String location, ParseResult result) {
33523335
if (obj == null) {
33533336
return null;
@@ -3729,8 +3712,7 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
37293712
? new ObjectSchema()
37303713
: new Schema();
37313714
}
3732-
//TODO change the unevaluatedProperties Field in core oas 3.1
3733-
// schema.setUnevaluatedProperties(unevaluatedProperties);
3715+
schema.setUnevaluatedProperties(unevaluatedProperties);
37343716
}
37353717

37363718
if (schema == null) {
@@ -3985,18 +3967,22 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
39853967
}
39863968
}
39873969

3988-
Map<String, Schema> dependentRequiredList = new LinkedHashMap<>();
3970+
Map<String, List<String>> dependentRequiredList = new LinkedHashMap<>();
39893971
ObjectNode dependentRequiredObj = getObject("dependentRequired", node, false, location, result);
3990-
Schema dependentRequired = null;
3972+
List<String> dependentRequired = new ArrayList<>();
39913973

39923974
Set<String> dependentRequiredKeys = getKeys(dependentRequiredObj);
39933975
for (String name : dependentRequiredKeys) {
39943976
JsonNode dependentRequiredValue = dependentRequiredObj.get(name);
3995-
if (!dependentRequiredValue.getNodeType().equals(JsonNodeType.OBJECT)) {
3977+
if (!dependentRequiredValue.getNodeType().equals(JsonNodeType.ARRAY)) {
39963978
result.invalidType(location, "dependentRequired", "object", dependentRequiredValue);
39973979
} else {
39983980
if (dependentRequiredObj != null) {
3999-
dependentRequired = getJsonSchema((ObjectNode) dependentRequiredValue, location, result);
3981+
for (JsonNode n : dependentRequiredValue){
3982+
if (n.getNodeType().equals(JsonNodeType.STRING)) {
3983+
dependentRequired.add(n.textValue());
3984+
}
3985+
}
40003986
if (dependentRequired != null) {
40013987
dependentRequiredList.put(name, dependentRequired);
40023988
}
@@ -4031,7 +4017,6 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
40314017

40324018
//prefixItems
40334019
ArrayNode prefixItemsArray = getArray("prefixItems", node, false, location, result);
4034-
40354020
if(prefixItemsArray != null) {
40364021
Schema prefixItems = new Schema();
40374022

@@ -4194,9 +4179,13 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
41944179
schema.setExternalDocs(docs);
41954180
}
41964181
}
4197-
//examples
41984182
ArrayNode examples = getArray("examples", node,false, location, result);
4199-
List<Example> exampleList = getExampleList(examples, location, result);
4183+
List<Object> exampleList = new ArrayList<>();
4184+
if (examples != null) {
4185+
for (JsonNode item : examples) {
4186+
exampleList.add(item);
4187+
}
4188+
}
42004189
if(exampleList.size() > 0){
42014190
schema.setExamples(exampleList);
42024191
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAI31DeserializationTest.java

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.swagger.v3.parser.core.models.SwaggerParseResult;
1010
import org.testng.annotations.Test;
1111

12+
import java.util.ArrayList;
1213
import java.util.List;
1314

1415
import static org.testng.Assert.*;
@@ -395,6 +396,8 @@ public void testSiblingsReferenceJSONSchema5() {
395396
" propertyNames:\n" +
396397
" pattern: ^[A-Za-z_][A-Za-z0-9_]*$\n" +
397398
" $ref: ./ex.json#user-profile\n" +
399+
" unevaluatedProperties:\n" +
400+
" type: object\n"+
398401
" Person:\n" +
399402
" type: array\n" +
400403
" prefixItems:\n" +
@@ -422,6 +425,7 @@ public void testSiblingsReferenceJSONSchema5() {
422425
Schema profile = openAPI.getComponents().getSchemas().get("Profile");
423426
Schema containsSchema = openAPI.getComponents().getSchemas().get("ContainsSchema");
424427
Schema personSchema = openAPI.getComponents().getSchemas().get("Person");
428+
Schema patientPersonSchema = openAPI.getComponents().getSchemas().get("PatientPerson");
425429
assertNotNull(profile.get$ref());
426430

427431
//contains
@@ -439,11 +443,15 @@ public void testSiblingsReferenceJSONSchema5() {
439443
//propertyNames
440444
assertEquals(profile.getPropertyNames().getPattern(),"^[A-Za-z_][A-Za-z0-9_]*$");
441445
//unevaluatedProperties
442-
//assertNotNull(profile.getUnevaluatedProperties());
443-
//assertTrue(profile.getUnevaluatedProperties().getTypes().contains("object"));
446+
assertNotNull(profile.getUnevaluatedProperties());
447+
assertTrue(profile.getUnevaluatedProperties() instanceof Schema);
448+
assertTrue(((Schema)profile.getUnevaluatedProperties()).getTypes().contains("object"));
449+
assertNotNull(patientPersonSchema.getUnevaluatedProperties());
450+
assertTrue(patientPersonSchema.getUnevaluatedProperties() instanceof Boolean);
451+
assertFalse(((Boolean)patientPersonSchema.getUnevaluatedProperties()).booleanValue());
444452
}
445453

446-
@Test(description = "Test siblings with $ref for if - then - else, dependentRequired, dependentSchemas, examples / example")
454+
@Test(description = "Test siblings with $ref for if - then - else, dependentRequired, dependentSchemas")
447455
public void testSiblingsReferenceJSONSchema6() {
448456
ParseOptions options = new ParseOptions();
449457
String refSibling = "openapi: 3.1.0\n" +
@@ -514,28 +522,10 @@ public void testSiblingsReferenceJSONSchema6() {
514522
" type: boolean\n" +
515523
" const: true\n" +
516524
" required:\n" +
517-
" - accept\n" +
518-
" Fruit:\n" +
519-
" type: string\n" +
520-
" examples:\n" +
521-
" - apple\n" +
522-
" - orange\n" +
523-
" Error:\n" +
524-
" type: object\n" +
525-
" properties:\n" +
526-
" code:\n" +
527-
" type: integer\n" +
528-
" message:\n" +
529-
" type: string\n" +
530-
" examples:\n" +
531-
" - code: 123\n" +
532-
" message: Oops...\n" +
533-
" - code: 456\n" +
534-
" message: Feature is not available for your plan\n";
525+
" - accept\n";
535526
SwaggerParseResult result = new OpenAPIV3Parser().readContents( refSibling , null, options);
536527
OpenAPI openAPI = result.getOpenAPI();
537528
assertNotNull(openAPI);
538-
539529
Yaml31.prettyPrint(openAPI);
540530
//if - then - else
541531
assertNotNull(openAPI.getComponents().getSchemas().get("IfTest"));
@@ -546,6 +536,7 @@ public void testSiblingsReferenceJSONSchema6() {
546536
assertTrue(itTest.getThen().getProperties().containsKey("maple_trees"));
547537
assertNotNull(itTest.getElse());
548538
assertTrue(itTest.getElse().getProperties().containsKey("accept"));
539+
549540
//dependentRequired
550541
assertNotNull(openAPI.getComponents().getSchemas().get("Payment"));
551542
Schema payment = openAPI.getComponents().getSchemas().get("Payment");
@@ -554,10 +545,56 @@ public void testSiblingsReferenceJSONSchema6() {
554545
//dependentSchemas
555546
assertNotNull(openAPI.getComponents().getSchemas().get("PaymentMethod"));
556547
Schema paymentMethod = openAPI.getComponents().getSchemas().get("PaymentMethod");
548+
}
549+
550+
@Test(description = "Test examples in JSONSchema")
551+
public void testExamplesJSONSchema() {
552+
ParseOptions options = new ParseOptions();
553+
String examplesSchema = "openapi: 3.1.0\n" +
554+
"info:\n" +
555+
" title: examples JSONSchema\n" +
556+
" version: 1.0.0\n" +
557+
"servers:\n" +
558+
" - url: /\n" +
559+
"paths: { }\n" +
560+
"components:\n" +
561+
" schemas:\n" +
562+
" Fruit:\n" +
563+
" type: string\n" +
564+
" example: kiwi\n" +
565+
" examples:\n" +
566+
" - apple\n" +
567+
" - orange\n" +
568+
" Error:\n" +
569+
" type: object\n" +
570+
" example: wrong\n" +
571+
" properties:\n" +
572+
" code:\n" +
573+
" type: integer\n" +
574+
" message:\n" +
575+
" type: string\n" +
576+
" examples:\n" +
577+
" - code: 123\n" +
578+
" message: Oops...\n" +
579+
" - code: 456\n" +
580+
" message: Feature is not available for your plan\n" +
581+
" ExampleSchema:\n" +
582+
" type: object\n" +
583+
" example: foo\n";
584+
SwaggerParseResult result = new OpenAPIV3Parser().readContents( examplesSchema , null, options);
585+
OpenAPI openAPI = result.getOpenAPI();
586+
assertNotNull(openAPI);
557587
//examples / example
558588
assertNotNull(openAPI.getComponents().getSchemas().get("Fruit"));
559589
Schema fruit = openAPI.getComponents().getSchemas().get("Fruit");
590+
assertTrue(fruit.getExample().equals("kiwi"));
591+
assertNotNull(fruit.getExamples());
560592
assertNotNull(openAPI.getComponents().getSchemas().get("Error"));
561593
Schema error = openAPI.getComponents().getSchemas().get("Error");
594+
assertTrue(error.getExample().equals("wrong"));
595+
assertTrue(error.getExamples().get(0)!= null);
596+
assertNotNull(openAPI.getComponents().getSchemas().get("ExampleSchema"));
597+
Schema exampleSchema = openAPI.getComponents().getSchemas().get("ExampleSchema");
598+
assertTrue(exampleSchema.getExample().equals("foo"));
562599
}
563600
}

0 commit comments

Comments
 (0)