Search in sources :

Example 1 with ApiDeclaration

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

the class OperationConverterTest method convertOperation1.

@Test
public void convertOperation1() throws Exception {
    io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation();
    operation.setMethod(Method.GET);
    operation.setSummary("the summary");
    operation.setNotes("the notes");
    operation.setNickname("getFun");
    List<String> produces = new ArrayList<String>();
    produces.add("application/json");
    operation.setProduces(produces);
    // response type
    operation.setRef("Cat");
    // response messages
    List<ResponseMessage> responses = new ArrayList<ResponseMessage>();
    ResponseMessage message400 = new ResponseMessage();
    message400.setCode(400);
    message400.setMessage("got a 400");
    responses.add(message400);
    operation.setResponseMessages(responses);
    // parameters
    io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
    param.setParamType(ParamType.QUERY);
    param.setDescription("a string query param");
    param.setRequired(false);
    param.setAllowMultiple(false);
    param.setType("string");
    List<io.swagger.models.apideclaration.Parameter> parameters = new ArrayList<io.swagger.models.apideclaration.Parameter>();
    parameters.add(param);
    operation.setParameters(parameters);
    Operation converted = converter.convertOperation("tag", operation, new ApiDeclaration());
    assertTrue(converted.getTags().size() == 1);
    assertEquals(converted.getTags().get(0), "tag");
    assertEquals(operation.getSummary(), converted.getSummary());
    assertEquals(operation.getNotes(), converted.getDescription());
    assertEquals(operation.getNickname(), converted.getOperationId());
    assertTrue(converted.getProduces().size() == 1);
    assertEquals(converted.getProduces().get(0), "application/json");
    assertTrue(converted.getParameters().size() == 1);
    assertTrue(converted.getResponses().size() == 2);
    Response response = converted.getResponses().get("200");
    assertNotNull(response);
    assertEquals(response.getDescription(), "success");
    Model schema = response.getResponseSchema();
    assertNotNull(schema);
    assertTrue(schema.getClass().equals(RefModel.class));
    RefModel ref = (RefModel) schema;
    assertEquals(ref.getSimpleRef(), "Cat");
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) RefModel(io.swagger.models.RefModel) Operation(io.swagger.models.Operation) ResponseMessage(io.swagger.models.apideclaration.ResponseMessage) Response(io.swagger.models.Response) RefModel(io.swagger.models.RefModel) Model(io.swagger.models.Model) Test(org.testng.annotations.Test)

Example 2 with ApiDeclaration

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

the class OperationConverterTest method testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration.

@Test
public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration() throws Exception {
    Set<String> expectedConsumes = new HashSet<>(Arrays.asList("application/json", "application/xml"));
    Set<String> expectedProduces = new HashSet<>(Arrays.asList("text/plain"));
    final ApiDeclaration apiDeclaration = new ApiDeclaration();
    apiDeclaration.setConsumes(new ArrayList<>(expectedConsumes));
    apiDeclaration.setProduces(new ArrayList<>(expectedProduces));
    io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation();
    operation.setMethod(Method.GET);
    final SwaggerCompatConverter swaggerCompatConverter = new SwaggerCompatConverter();
    Operation converted = swaggerCompatConverter.convertOperation("tag", operation, apiDeclaration);
    assertSetsAreEqual(expectedConsumes, converted.getConsumes());
    assertSetsAreEqual(expectedProduces, converted.getProduces());
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) SwaggerCompatConverter(io.swagger.parser.SwaggerCompatConverter) Operation(io.swagger.models.Operation) Test(org.testng.annotations.Test)

Example 3 with ApiDeclaration

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

the class ResourceListingConverterTest method convertResourceListingWithRootPath.

@Test
public void convertResourceListingWithRootPath() throws Exception {
    ResourceListing rl = new ResourceListing();
    rl.setApiVersion("2.11");
    List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
    ApiDeclaration api = new ApiDeclaration();
    api.setBasePath("http://foo.com");
    apis.add(api);
    Swagger swagger = converter.convert(rl, apis);
    assertTrue(swagger.getSchemes().size() == 1);
    assertTrue(swagger.getSwagger().equals("2.0"));
    Info info = swagger.getInfo();
    assertNotNull(info);
    assertEquals(info.getVersion(), rl.getApiVersion());
    assertEquals(swagger.getBasePath(), "/");
    assertEquals(swagger.getHost(), "foo.com");
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ResourceListing(io.swagger.models.resourcelisting.ResourceListing) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) Info(io.swagger.models.Info) Test(org.testng.annotations.Test)

Example 4 with ApiDeclaration

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

the class ResourceListingReader method main.

public static void main(String[] args) throws IOException, URISyntaxException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
    String baseUrl = "http://petstore.swagger.io/api/api-docs";
    ResourceListing resourceListing = objectMapper.readValue(new URL(baseUrl), ResourceListing.class);
    Map<String, ApiDeclaration> apiDeclarations = new HashMap<>();
    List<ApiListingReference> apis = resourceListing.getApis();
    if (apis != null) {
        for (ApiListingReference api : apis) {
            URL apiUrl;
            URI uri = new URI(api.getPath());
            if (uri.isAbsolute()) {
                apiUrl = uri.toURL();
            } else {
                apiUrl = new URL(baseUrl + api.getPath());
            }
            apiDeclarations.put(api.getPath(), objectMapper.readValue(apiUrl, ApiDeclaration.class));
        }
    }
    System.out.println("---=== Resource Listing (" + baseUrl + ") ==--");
    System.out.println(resourceListing);
    for (Map.Entry<String, ApiDeclaration> apiDeclarationEntry : apiDeclarations.entrySet()) {
        System.out.println("---=== API Declaration (" + apiDeclarationEntry.getKey() + ") ==--");
        System.out.println(apiDeclarationEntry.getValue());
    }
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ResourceListing(io.swagger.models.resourcelisting.ResourceListing) HashMap(java.util.HashMap) ApiListingReference(io.swagger.models.resourcelisting.ApiListingReference) URI(java.net.URI) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL)

Example 5 with ApiDeclaration

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

the class SwaggerCompatConverter method read.

@Override
public Swagger read(String input, List<AuthorizationValue> auths) throws IOException {
    Swagger output = null;
    MessageBuilder migrationMessages = new MessageBuilder();
    SwaggerLegacyParser swaggerParser = new SwaggerLegacyParser();
    ResourceListing resourceListing = null;
    resourceListing = readResourceListing(input, migrationMessages, auths);
    List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
    if (resourceListing != null) {
        List<ApiListingReference> refs = resourceListing.getApis();
        boolean readAsSingleFile = false;
        if (refs != null) {
            for (ApiListingReference ref : refs) {
                ApiDeclaration apiDeclaration = null;
                JsonNode node = ref.getExtraFields();
                JsonNode operations = node.get("operations");
                if (operations != null) {
                    if (!readAsSingleFile) {
                        // this is a single-file swagger definition
                        apiDeclaration = readDeclaration(input, migrationMessages, auths);
                        // avoid doing this again
                        readAsSingleFile = true;
                    }
                } else {
                    String location = null;
                    if (input.startsWith("http")) {
                        // look up as url
                        String pathLocation = ref.getPath();
                        if (pathLocation.startsWith("http")) {
                            // use as absolute url
                            location = pathLocation;
                        } else {
                            if (pathLocation.startsWith("/")) {
                                // handle 1.1 specs
                                if (resourceListing.getSwaggerVersion().equals(SwaggerVersion.V1_1) && resourceListing.getExtraFields().get("basePath") != null) {
                                    String basePath = resourceListing.getExtraFields().get("basePath").textValue();
                                    location = basePath + pathLocation;
                                } else {
                                    location = input + pathLocation;
                                }
                            } else {
                                location = input + "/" + pathLocation;
                            }
                        }
                    } else {
                        // file system
                        File fileLocation = new File(input);
                        if (ref.getPath().startsWith("/")) {
                            location = fileLocation.getParent() + ref.getPath();
                        } else {
                            location = fileLocation.getParent() + File.separator + ref.getPath();
                        }
                    }
                    if (location.indexOf(".{format}") != -1) {
                        location = location.replaceAll("\\.\\{format\\}", ".json");
                    }
                    apiDeclaration = readDeclaration(location, migrationMessages, auths);
                }
                if (apiDeclaration != null) {
                    apis.add(apiDeclaration);
                }
            }
        }
        output = convert(resourceListing, apis);
    }
    return output;
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiListingReference(io.swagger.models.resourcelisting.ApiListingReference) ResourceListing(io.swagger.models.resourcelisting.ResourceListing) MessageBuilder(io.swagger.report.MessageBuilder) Swagger(io.swagger.models.Swagger) File(java.io.File)

Aggregations

ApiDeclaration (io.swagger.models.apideclaration.ApiDeclaration)11 Swagger (io.swagger.models.Swagger)4 ResourceListing (io.swagger.models.resourcelisting.ResourceListing)4 Test (org.testng.annotations.Test)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Info (io.swagger.models.Info)3 Operation (io.swagger.models.Operation)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Model (io.swagger.models.Model)2 RefModel (io.swagger.models.RefModel)2 Api (io.swagger.models.apideclaration.Api)2 ApiListingReference (io.swagger.models.resourcelisting.ApiListingReference)2 Message (io.swagger.report.Message)2 MessageBuilder (io.swagger.report.MessageBuilder)2 ApiDeclarationMigrator (io.swagger.transform.migrate.ApiDeclarationMigrator)2 URL (java.net.URL)2 Map (java.util.Map)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1