Search in sources :

Example 36 with SwaggerDeserializationResult

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

the class Swagger20Parser method convertToSwagger.

private Swagger convertToSwagger(String data) throws IOException {
    if (data != null) {
        JsonNode rootNode;
        if (data.trim().startsWith("{")) {
            ObjectMapper mapper = Json.mapper();
            rootNode = mapper.readTree(data);
        } else {
            rootNode = DeserializationUtils.readYamlTree(data);
        }
        if (System.getProperty("debugParser") != null) {
            LOGGER.info("\n\nSwagger Tree: \n" + ReflectionToStringBuilder.toString(rootNode, ToStringStyle.MULTI_LINE_STYLE) + "\n\n");
        }
        if (rootNode == null) {
            return null;
        }
        // must have swagger node set
        JsonNode swaggerNode = rootNode.get("swagger");
        if (swaggerNode == null) {
            return null;
        } else {
            SwaggerDeserializationResult result = new SwaggerDeserializer().deserialize(rootNode);
            Swagger convertValue = result.getSwagger();
            if (System.getProperty("debugParser") != null) {
                LOGGER.info("\n\nSwagger Tree convertValue : \n" + ReflectionToStringBuilder.toString(convertValue, ToStringStyle.MULTI_LINE_STYLE) + "\n\n");
            }
            return convertValue;
        }
    } else {
        return null;
    }
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SwaggerDeserializer(io.swagger.parser.util.SwaggerDeserializer)

Example 37 with SwaggerDeserializationResult

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

the class SwaggerParser method readWithInfo.

public SwaggerDeserializationResult readWithInfo(String location, List<AuthorizationValue> auths, boolean resolve) {
    if (location == null) {
        return null;
    }
    location = location.replaceAll("\\\\", "/");
    List<SwaggerParserExtension> parserExtensions = getExtensions();
    SwaggerDeserializationResult output;
    if (auths == null) {
        auths = new ArrayList<AuthorizationValue>();
    }
    output = new Swagger20Parser().readWithInfo(location, auths);
    if (output != null) {
        if (output.getSwagger() != null && "2.0".equals(output.getSwagger().getSwagger())) {
            if (resolve) {
                output.setSwagger(new SwaggerResolver(output.getSwagger(), auths, location).resolve());
            }
            return output;
        }
    }
    for (SwaggerParserExtension extension : parserExtensions) {
        output = extension.readWithInfo(location, auths);
        if (output != null && output.getSwagger() != null && "2.0".equals(output.getSwagger().getSwagger())) {
            return output;
        }
    }
    if (output == null) {
        output = new SwaggerDeserializationResult().message("The swagger definition could not be read");
    }
    return output;
}
Also used : AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult)

Example 38 with SwaggerDeserializationResult

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

the class FileReferenceTests method testIssue323.

@Test
public void testIssue323() {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-323.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/events"));
    assertNotNull(swagger.getDefinitions().get("StatusResponse"));
    assertNotNull(swagger.getDefinitions().get("Paging"));
    assertNotNull(swagger.getDefinitions().get("Foobar"));
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Test(org.testng.annotations.Test)

Example 39 with SwaggerDeserializationResult

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

the class FileReferenceTests method testIssue421.

@Test
public void testIssue421() {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-421.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/pet/{petId}"));
    assertNotNull(swagger.getPath("/pet/{petId}").getGet());
    assertNotNull(swagger.getPath("/pet/{petId}").getGet().getParameters());
    assertTrue(swagger.getPath("/pet/{petId}").getGet().getParameters().size() == 1);
    assertTrue(swagger.getPath("/pet/{petId}").getGet().getParameters().get(0).getName().equals("petId"));
    assertTrue(swagger.getDefinitions().get("Pet") instanceof ModelImpl);
    assertTrue(swagger.getDefinitions().get("Pet").getProperties().size() == 6);
    assertNotNull(swagger.getPath("/pet/{petId}").getPost());
    assertNotNull(swagger.getPath("/pet/{petId}").getPost().getParameters());
    assertTrue(swagger.getPath("/pet/{petId}").getPost().getParameters().size() == 3);
    assertTrue(swagger.getPath("/pet/{petId}").getPost().getParameters().get(1) instanceof RefParameter);
    assertTrue(((RefParameter) swagger.getPath("/pet/{petId}").getPost().getParameters().get(1)).getRefFormat() == RefFormat.INTERNAL);
    assertTrue(((RefParameter) swagger.getPath("/pet/{petId}").getPost().getParameters().get(1)).getSimpleRef().equals("name"));
    assertNotNull(swagger.getPath("/store/order"));
    assertNotNull(swagger.getPath("/store/order").getPost());
    assertNotNull(swagger.getPath("/store/order").getPost().getParameters());
    assertTrue(swagger.getPath("/store/order").getPost().getParameters().size() == 1);
    assertTrue(swagger.getPath("/store/order").getPost().getParameters().get(0) instanceof BodyParameter);
    assertNotNull(((BodyParameter) swagger.getPath("/store/order").getPost().getParameters().get(0)).getSchema());
    assertTrue(((BodyParameter) swagger.getPath("/store/order").getPost().getParameters().get(0)).getSchema() instanceof RefModel);
    assertTrue(((RefModel) ((BodyParameter) swagger.getPath("/store/order").getPost().getParameters().get(0)).getSchema()).getSimpleRef().equals("Order"));
    assertTrue(swagger.getDefinitions().get("Order") instanceof ModelImpl);
    assertTrue(swagger.getDefinitions().get("Order").getProperties().size() == 6);
}
Also used : RefParameter(io.swagger.models.parameters.RefParameter) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Example 40 with SwaggerDeserializationResult

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

the class FileReferenceTests method testIssue289.

@Test
public void testIssue289() {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/issue-289.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/foo").getGet());
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) 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