diff --git a/src/test/java/io/swagger/test/utils/ResolverUtilTest.java b/src/test/java/io/swagger/test/utils/ResolverUtilTest.java index 533b6375..1ec80b90 100644 --- a/src/test/java/io/swagger/test/utils/ResolverUtilTest.java +++ b/src/test/java/io/swagger/test/utils/ResolverUtilTest.java @@ -13,6 +13,9 @@ import io.swagger.parser.SwaggerParser; import io.swagger.sample.models.Dog; import io.swagger.util.Json; +import org.apache.commons.io.IOUtils; +import org.testng.Assert; +import org.testng.annotations.DataProvider; import io.swagger.util.Yaml; import org.testng.annotations.Test; @@ -21,7 +24,12 @@ import static org.testng.Assert.fail; import static org.testng.AssertJUnit.assertEquals; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + public class ResolverUtilTest { + + private static final String REQUIRED_PROPERTY = "requiredProperty"; @Test @@ -66,6 +74,7 @@ public void testCircularRefs() { } + @Test public void testArrayParam() { Swagger swagger = new SwaggerParser().read("./src/test/swagger/sample1.yaml"); @@ -282,6 +291,39 @@ public void testSelfReferenceResolution() { } + @Test(dataProvider = REQUIRED_PROPERTY) + public void testRequiredProperty(String yml, final String model, final String property, + final boolean required) throws Exception { + final Swagger swagger = new SwaggerParser().parse(yml); + final Runnable checker = new Runnable() { + @Override + public void run() { + final boolean actual = swagger.getDefinitions().get(model).getProperties() + .get(property).getRequired(); + if (required) { + Assert.assertTrue(actual, + String.format("The %s.%s is mandatory", model, property)); + } else { + Assert.assertFalse(actual, + String.format("The %s.%s is optional", model, property)); + } + } + }; + checker.run(); + new ResolverUtil().resolveFully(swagger); + checker.run(); + } + + @DataProvider(name = REQUIRED_PROPERTY) + private static Object[][] listRequiredProperties() throws IOException { + final String yml = IOUtils.toString(ResolverUtilTest.class.getResource("shared-model.yml"), + StandardCharsets.UTF_8); + return new Object[][] { + {yml, "One", "nested", false}, + {yml, "Two", "nested", true} + }; + + @Test public void testResolvingWithoutDefinitions() { String yaml = @@ -301,5 +343,6 @@ public void testResolvingWithoutDefinitions() { Swagger swagger = new SwaggerParser().parse(yaml); new ResolverUtil().resolveFully(swagger); + } } diff --git a/src/test/resources/io/swagger/test/utils/shared-model.yml b/src/test/resources/io/swagger/test/utils/shared-model.yml new file mode 100644 index 00000000..21c3e7eb --- /dev/null +++ b/src/test/resources/io/swagger/test/utils/shared-model.yml @@ -0,0 +1,54 @@ +swagger: '2.0' +info: + description: Test + version: "1" + title: test +paths: + '/one': + put: + operationId: one + parameters: + - in: body + name: body + required: false + schema: + $ref: '#/definitions/One' + responses: + default: + description: successful operation + '/two': + post: + operationId: two + parameters: + - in: body + name: body + required: false + schema: + $ref: '#/definitions/Two' + responses: + default: + description: successful operation +definitions: + Shared: + type: object + required: + - type + properties: + type: + type: string + One: + type: object + properties: + nested: + $ref: '#/definitions/Shared' + one: + type: string + Two: + type: object + required: + - nested + properties: + nested: + $ref: '#/definitions/Shared' + two: + type: string