Search in sources :

Example 1 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 swaggerAsString, boolean resolve) {
    if (swaggerAsString == null) {
        return new SwaggerDeserializationResult().message("empty or null swagger supplied");
    }
    try {
        JsonNode node;
        if (swaggerAsString.trim().startsWith("{")) {
            ObjectMapper mapper = Json.mapper();
            node = mapper.readTree(swaggerAsString);
        } else {
            node = DeserializationUtils.readYamlTree(swaggerAsString);
        }
        SwaggerDeserializationResult result = new Swagger20Parser().readWithInfo(node);
        if (result != null) {
            if (resolve) {
                result.setSwagger(new SwaggerResolver(result.getSwagger(), new ArrayList<AuthorizationValue>(), null).resolve());
            }
        } else {
            result = new SwaggerDeserializationResult().message("Definition does not appear to be a valid Swagger format");
        }
        return result;
    } catch (Exception e) {
        return new SwaggerDeserializationResult().message("malformed or unreadable swagger supplied");
    }
}
Also used : AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException)

Example 2 with SwaggerDeserializationResult

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

the class AnchorTest method testIssue146.

@Test
public void testIssue146() {
    String yaml = "swagger: '2.0'\n" + "\n" + "info:\n" + "  version: \"0.0.1\"\n" + "  title: API\n" + "x-types:\n" + "  OperationType: &OperationType\n" + "    - registration\n" + "# Describe your paths here\n" + "paths:\n" + "  /checker:\n" + "    get:\n" + "      parameters:\n" + "        - name: operations\n" + "          in: query\n" + "          type: array\n" + "          items:\n" + "            type: string\n" + "            enum: *OperationType\n" + "          default: [registration]\n" + "      responses:\n" + "        200:\n" + "          description: OK\n" + "          schema:\n" + "            $ref: '#/definitions/OperationType'\n" + "definitions:\n" + "  OperationType:\n" + "    type: string\n" + "    enum: *OperationType";
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(yaml);
    ModelImpl model = (ModelImpl) result.getSwagger().getDefinitions().get("OperationType");
    assertEquals(model.getEnum(), Arrays.asList("registration"));
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) ModelImpl(io.swagger.models.ModelImpl) Test(org.testng.annotations.Test)

Example 3 with SwaggerDeserializationResult

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

the class FileReferenceTests method testIssue306.

@Test
public void testIssue306() {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-306.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertTrue(swagger.getDefinitions().size() == 3);
    // resolved from `$ref: './book.yaml'`
    assertNotNull(swagger.getDefinitions().get("Inventory"));
    // resolved from `$ref: 'book.yaml'`
    assertNotNull(swagger.getDefinitions().get("Orders"));
    // copied from `./book.yaml`
    assertNotNull(swagger.getDefinitions().get("book"));
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Test(org.testng.annotations.Test)

Example 4 with SwaggerDeserializationResult

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

the class FileReferenceTests method testIssue314.

@Test
public void testIssue314() {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-314.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/events"));
    Path path = swagger.getPath("/events");
    assertNotNull(path.getGet());
    Operation get = path.getGet();
    assertEquals(get.getOperationId(), "getEvents");
    assertTrue(swagger.getDefinitions().size() == 3);
    assertTrue(swagger.getDefinitions().get("Foobar").getProperties().size() == 1);
    assertTrue(swagger.getDefinitions().get("StatusResponse").getProperties().size() == 1);
    assertTrue(swagger.getDefinitions().get("Paging").getProperties().size() == 1);
}
Also used : SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Test(org.testng.annotations.Test)

Example 5 with SwaggerDeserializationResult

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

the class FileReferenceTests method testIssue310.

@Test
public void testIssue310() {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-310.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertTrue(swagger.getDefinitions().size() == 2);
    assertTrue(swagger.getDefinitions().get("Paging").getProperties().size() == 1);
}
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