Search in sources :

Example 16 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult 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)

Example 17 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class SwaggerParserTest method testIssue292WithNoCollectionFormat.

@Test
public void testIssue292WithNoCollectionFormat() {
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  version: '0.0.0'\n" + "  title: nada\n" + "paths:\n" + "  /persons:\n" + "    get:\n" + "      parameters:\n" + "      - name: testParam\n" + "        in: query\n" + "        type: array\n" + "        items:\n" + "          type: string\n" + "      responses:\n" + "        200:\n" + "          description: Successful response";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Swagger swagger = result.getSwagger();
    Parameter param = swagger.getPaths().get("/persons").getGet().getParameters().get(0);
    QueryParameter qp = (QueryParameter) param;
    assertNull(qp.getCollectionFormat());
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) 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) Test(org.testng.annotations.Test)

Example 18 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class SwaggerParserTest method testIncompatibleRefs.

@Test
public void testIncompatibleRefs() {
    String yaml = "swagger: '2.0'\n" + "paths:\n" + "  /test:\n" + "    post:\n" + "      parameters:\n" + "        # this is not the correct reference type\n" + "        - $ref: '#/definitions/Model'\n" + "        - in: body\n" + "          name: incorrectType\n" + "          required: true\n" + "          schema:\n" + "            $ref: '#/definitions/Model'\n" + "      responses:\n" + "        200:\n" + "          # this is not the correct reference type\n" + "          $ref: '#/definitions/Model'\n" + "        400:\n" + "          definitions: this is right\n" + "          schema:\n" + "            $ref: '#/definitions/Model'\n" + "definitions:\n" + "  Model:\n" + "    type: object";
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(yaml);
    assertNotNull(result.getSwagger());
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Test(org.testng.annotations.Test)

Example 19 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class SwaggerParserTest method testIssue393.

@Test
public void testIssue393() {
    SwaggerParser parser = new SwaggerParser();
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  title: x\n" + "  version: 1.0.0\n" + "paths:\n" + "  /test:\n" + "    get:\n" + "      parameters: []\n" + "      responses:\n" + "        '400':\n" + "          description: |\n" + "            The account could not be created because a credential didn't meet the complexity requirements.\n" + "          x-error-refs:\n" + "            - '$ref': '#/x-error-defs/credentialTooShort'\n" + "            - '$ref': '#/x-error-defs/credentialTooLong'\n" + "x-error-defs:\n" + "  credentialTooShort:\n" + "    errorID: credentialTooShort";
    final SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getVendorExtensions().get("x-error-defs"));
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 20 with SwaggerDeserializationResult

use of io.swagger.parser.util.SwaggerDeserializationResult in project swagger-parser by swagger-api.

the class SwaggerReaderTest method testIssue192.

@Test
public void testIssue192() {
    String spec = "swagger: '2.0'\n" + "info:\n" + "  title: issue 192\n" + "paths:\n" + "  /foo:\n" + "    get:\n" + "      parameters:\n" + "        - name: Code\n" + "          in: path\n" + "          description: The code\n" + "          required: true\n" + "          type: string\n" + "          minLength: 4\n" + "          maxLength: 5\n" + "          pattern: '^[a-zA-Z]'\n" + "      responses:\n" + "        200:\n" + "          description: 'the pet'";
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(spec);
    Swagger swagger = result.getSwagger();
    Parameter param = swagger.getPath("/foo").getGet().getParameters().get(0);
    assertTrue(param instanceof PathParameter);
    PathParameter pathParameter = (PathParameter) param;
    assertEquals(pathParameter.getMinLength(), new Integer(4));
    assertEquals(pathParameter.getMaxLength(), new Integer(5));
    assertNull(pathParameter.isReadOnly());
    assertEquals(pathParameter.getPattern(), "^[a-zA-Z]");
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) PathParameter(io.swagger.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Aggregations

SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)56 Test (org.testng.annotations.Test)44 Swagger (io.swagger.models.Swagger)21 AuthorizationValue (io.swagger.models.auth.AuthorizationValue)10 SwaggerParser (io.swagger.parser.SwaggerParser)8 Expectations (mockit.Expectations)8 Parameter (io.swagger.models.parameters.Parameter)7 PathParameter (io.swagger.models.parameters.PathParameter)7 QueryParameter (io.swagger.models.parameters.QueryParameter)7 RefProperty (io.swagger.models.properties.RefProperty)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ArrayModel (io.swagger.models.ArrayModel)5 Model (io.swagger.models.Model)5 RefModel (io.swagger.models.RefModel)5 BodyParameter (io.swagger.models.parameters.BodyParameter)5 ComposedModel (io.swagger.models.ComposedModel)4 Path (io.swagger.models.Path)4 ArrayProperty (io.swagger.models.properties.ArrayProperty)4 Property (io.swagger.models.properties.Property)4 IOException (java.io.IOException)4