Search in sources :

Example 11 with ArrayModel

use of io.swagger.models.ArrayModel in project swagger-parser by swagger-api.

the class SwaggerCompatConverterTest method testIssue_641.

@Test
public void testIssue_641() throws IOException {
    SwaggerCompatConverter converter = new SwaggerCompatConverter();
    Swagger swagger2 = converter.read("specs/v1_2/issue-641.json");
    assertNotNull(swagger2);
    BodyParameter bodyParameter = (BodyParameter) swagger2.getPaths().get("/user/createWithArray").getPost().getParameters().get(0);
    assertEquals(((RefProperty) ((ArrayModel) bodyParameter.getSchema()).getItems()).get$ref(), "#/definitions/User");
}
Also used : SwaggerCompatConverter(io.swagger.parser.SwaggerCompatConverter) Swagger(io.swagger.models.Swagger) BodyParameter(io.swagger.models.parameters.BodyParameter) ArrayModel(io.swagger.models.ArrayModel) Test(org.testng.annotations.Test)

Example 12 with ArrayModel

use of io.swagger.models.ArrayModel in project swagger-parser by swagger-api.

the class SwaggerParserTest method testIssue450.

@Test
public void testIssue450() {
    String desc = "An array of Pets";
    String xTag = "x-my-tag";
    String xVal = "An extension tag";
    String yaml = "swagger: \"2.0\"\n" + "info:\n" + "  version: 0.0.0\n" + "  title: Simple API\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "definitions:\n" + "  PetArray:\n" + "    type: array\n" + "    items:\n" + "      $ref: \"#/definitions/Pet\"\n" + "    description: " + desc + "\n" + "    " + xTag + ": " + xVal + "\n" + "  Pet:\n" + "    type: object\n" + "    properties:\n" + "      id:\n" + "        type: string";
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(yaml);
    assertNotNull(result.getSwagger());
    final Swagger swagger = result.getSwagger();
    Model petArray = swagger.getDefinitions().get("PetArray");
    assertNotNull(petArray);
    assertTrue(petArray instanceof ArrayModel);
    assertEquals(petArray.getDescription(), desc);
    assertNotNull(petArray.getVendorExtensions());
    assertNotNull(petArray.getVendorExtensions().get(xTag));
    assertEquals(petArray.getVendorExtensions().get(xTag), xVal);
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) ArrayModel(io.swagger.models.ArrayModel) Test(org.testng.annotations.Test)

Example 13 with ArrayModel

use of io.swagger.models.ArrayModel in project swagger-parser by swagger-api.

the class SwaggerParserTest method testDefinitionExample.

@Test
public void testDefinitionExample() throws Exception {
    SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.read(TestUtils.getResourceAbsolutePath("/definition_example.yaml"));
    ModelImpl model;
    ArrayModel arrayModel;
    model = (ModelImpl) swagger.getDefinitions().get("NumberType");
    assertEquals((Double) model.getExample(), 2.0d, 0d);
    model = (ModelImpl) swagger.getDefinitions().get("IntegerType");
    assertEquals((int) model.getExample(), 2);
    model = (ModelImpl) swagger.getDefinitions().get("StringType");
    assertEquals((String) model.getExample(), "2");
    model = (ModelImpl) swagger.getDefinitions().get("ObjectType");
    assertTrue(model.getExample() instanceof Map);
    Map objectExample = (Map) model.getExample();
    assertEquals((String) objectExample.get("propertyA"), "valueA");
    assertEquals((Integer) objectExample.get("propertyB"), new Integer(123));
    arrayModel = (ArrayModel) swagger.getDefinitions().get("ArrayType");
    assertTrue(arrayModel.getExample() instanceof List);
    List<Map> arrayExample = (List<Map>) arrayModel.getExample();
    assertEquals((String) arrayExample.get(0).get("propertyA"), "valueA1");
    assertEquals((Integer) arrayExample.get(0).get("propertyB"), new Integer(123));
    assertEquals((String) arrayExample.get(1).get("propertyA"), "valueA2");
    assertEquals((Integer) arrayExample.get(1).get("propertyB"), new Integer(456));
    model = (ModelImpl) swagger.getDefinitions().get("NumberTypeStringExample");
    assertEquals((String) model.getExample(), "2.0");
    model = (ModelImpl) swagger.getDefinitions().get("IntegerTypeStringExample");
    assertEquals((String) model.getExample(), "2");
    model = (ModelImpl) swagger.getDefinitions().get("StringTypeStringExample");
    assertEquals((String) model.getExample(), "2");
    model = (ModelImpl) swagger.getDefinitions().get("ObjectTypeStringExample");
    assertEquals((String) model.getExample(), "{\"propertyA\": \"valueA\", \"propertyB\": 123}");
    arrayModel = (ArrayModel) swagger.getDefinitions().get("ArrayTypeStringExample");
    assertEquals((String) arrayModel.getExample(), "[{\"propertyA\": \"valueA1\", \"propertyB\": 123}, {\"propertyA\": \"valueA2\", \"propertyB\": 456}]");
}
Also used : Swagger(io.swagger.models.Swagger) List(java.util.List) ModelImpl(io.swagger.models.ModelImpl) ArrayModel(io.swagger.models.ArrayModel) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 14 with ArrayModel

use of io.swagger.models.ArrayModel in project swagger-parser by swagger-api.

the class SwaggerParserTest method testIssue75.

@Test
public void testIssue75() {
    SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.read("src/test/resources/issue99.json");
    BodyParameter param = (BodyParameter) swagger.getPaths().get("/albums").getPost().getParameters().get(0);
    Model model = param.getSchema();
    assertNotNull(model);
    assertTrue(model instanceof ArrayModel);
    ArrayModel am = (ArrayModel) model;
    assertTrue(am.getItems() instanceof ByteArrayProperty);
    assertEquals(am.getItems().getFormat(), "byte");
}
Also used : ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) BodyParameter(io.swagger.models.parameters.BodyParameter) ArrayModel(io.swagger.models.ArrayModel) Test(org.testng.annotations.Test)

Example 15 with ArrayModel

use of io.swagger.models.ArrayModel in project swagger-parser by swagger-api.

the class SwaggerParserTest method doRelativeFileTest.

private Swagger doRelativeFileTest(String location) {
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult readResult = parser.readWithInfo(location, null, true);
    if (readResult.getMessages().size() > 0) {
        Json.prettyPrint(readResult.getMessages());
    }
    final Swagger swagger = readResult.getSwagger();
    final Path path = swagger.getPath("/health");
    // we successfully converted the RefPath to a Path
    assertEquals(path.getClass(), Path.class);
    final List<Parameter> parameters = path.getParameters();
    assertParamDetails(parameters, 0, QueryParameter.class, "param1", "query");
    assertParamDetails(parameters, 1, HeaderParameter.class, "param2", "header");
    final Operation operation = path.getGet();
    final List<Parameter> operationParams = operation.getParameters();
    assertParamDetails(operationParams, 0, PathParameter.class, "param3", "path");
    assertParamDetails(operationParams, 1, HeaderParameter.class, "param4", "header");
    assertParamDetails(operationParams, 2, BodyParameter.class, "body", "body");
    final BodyParameter bodyParameter = (BodyParameter) operationParams.get(2);
    assertEquals(((RefModel) bodyParameter.getSchema()).get$ref(), "#/definitions/health");
    final Map<String, Response> responsesMap = operation.getResponses();
    assertResponse(swagger, responsesMap, "200", "Health information from the server", "#/definitions/health");
    assertResponse(swagger, responsesMap, "400", "Your request was not valid", "#/definitions/error");
    assertResponse(swagger, responsesMap, "500", "An unexpected error occur during processing", "#/definitions/error");
    final Map<String, Model> definitions = swagger.getDefinitions();
    final ModelImpl refInDefinitions = (ModelImpl) definitions.get("refInDefinitions");
    assertEquals(refInDefinitions.getDescription(), "The example model");
    expectedPropertiesInModel(refInDefinitions, "foo", "bar");
    final ArrayModel arrayModel = (ArrayModel) definitions.get("arrayModel");
    final RefProperty arrayModelItems = (RefProperty) arrayModel.getItems();
    assertEquals(arrayModelItems.get$ref(), "#/definitions/foo");
    final ModelImpl fooModel = (ModelImpl) definitions.get("foo");
    assertEquals(fooModel.getDescription(), "Just another model");
    expectedPropertiesInModel(fooModel, "hello", "world");
    final ComposedModel composedCat = (ComposedModel) definitions.get("composedCat");
    final ModelImpl child = (ModelImpl) composedCat.getChild();
    expectedPropertiesInModel(child, "huntingSkill", "prop2", "reflexes", "reflexMap");
    final ArrayProperty reflexes = (ArrayProperty) child.getProperties().get("reflexes");
    final RefProperty reflexItems = (RefProperty) reflexes.getItems();
    assertEquals(reflexItems.get$ref(), "#/definitions/reflex");
    assertTrue(definitions.containsKey(reflexItems.getSimpleRef()));
    final MapProperty reflexMap = (MapProperty) child.getProperties().get("reflexMap");
    final RefProperty reflexMapAdditionalProperties = (RefProperty) reflexMap.getAdditionalProperties();
    assertEquals(reflexMapAdditionalProperties.get$ref(), "#/definitions/reflex");
    assertEquals(composedCat.getInterfaces().size(), 2);
    assertEquals(composedCat.getInterfaces().get(0).get$ref(), "#/definitions/pet");
    assertEquals(composedCat.getInterfaces().get(1).get$ref(), "#/definitions/foo_2");
    return swagger;
}
Also used : Path(io.swagger.models.Path) ArrayProperty(io.swagger.models.properties.ArrayProperty) ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) ComposedModel(io.swagger.models.ComposedModel) MapProperty(io.swagger.models.properties.MapProperty) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) SerializableParameter(io.swagger.models.parameters.SerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FormParameter(io.swagger.models.parameters.FormParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) ModelImpl(io.swagger.models.ModelImpl) ArrayModel(io.swagger.models.ArrayModel)

Aggregations

ArrayModel (io.swagger.models.ArrayModel)27 Test (org.testng.annotations.Test)21 Model (io.swagger.models.Model)17 BodyParameter (io.swagger.models.parameters.BodyParameter)17 Operation (io.swagger.models.Operation)15 RefProperty (io.swagger.models.properties.RefProperty)14 Property (io.swagger.models.properties.Property)10 Path (io.swagger.models.Path)9 RefModel (io.swagger.models.RefModel)9 StringProperty (io.swagger.models.properties.StringProperty)9 Swagger (io.swagger.models.Swagger)7 ArrayProperty (io.swagger.models.properties.ArrayProperty)6 ComposedModel (io.swagger.models.ComposedModel)4 ModelImpl (io.swagger.models.ModelImpl)4 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)4 Response (io.swagger.models.Response)3 ApiOperation (io.swagger.annotations.ApiOperation)2 FormParameter (io.swagger.models.parameters.FormParameter)2 HeaderParameter (io.swagger.models.parameters.HeaderParameter)2 Parameter (io.swagger.models.parameters.Parameter)2