Search in sources :

Example 11 with ResolverCache

use of io.swagger.v3.parser.ResolverCache in project swagger-parser by swagger-api.

the class ResolverCacheTest method testLoadInternalResponseRef.

@Test
public void testLoadInternalResponseRef(@Injectable ApiResponse mockedResponse) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addResponses("foo", mockedResponse));
    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    ApiResponse actualResult = cache.loadRef("#/components/responses/foo", RefFormat.INTERNAL, ApiResponse.class);
    assertEquals(actualResult, mockedResponse);
    assertNull(cache.loadRef("#/components/responses/bar", RefFormat.INTERNAL, ApiResponse.class));
}
Also used : Components(io.swagger.v3.oas.models.Components) ResolverCache(io.swagger.v3.parser.ResolverCache) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 12 with ResolverCache

use of io.swagger.v3.parser.ResolverCache in project swagger-parser by swagger-api.

the class ResolverCacheTest method testLoadExternalRefWithEscapedCharacters.

@Test
public void testLoadExternalRefWithEscapedCharacters() throws Exception {
    final RefFormat format = RefFormat.URL;
    final String ref = "http://my.company.com/path/to/main.yaml";
    final String contentsOfExternalFile = "openAPI: \"2.0\"\n" + "\n" + "info:\n" + "  version: 1.0.0\n" + "  title: Path include test case child\n" + "\n" + "paths:\n" + "  /foo~bar~1:\n" + "    get:\n" + "      responses:\n" + "        200:\n" + "          description: \"Request successful\"\n";
    new Expectations() {

        {
            RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
            times = 1;
            result = contentsOfExternalFile;
        }
    };
    ResolverCache cache = new ResolverCache(openAPI, auths, "http://my.company.com/path/parent.json");
    PathItem path = cache.loadRef(ref + "#/paths/~1foo~0bar~01", RefFormat.URL, PathItem.class);
    assertNotNull(path);
}
Also used : Expectations(mockit.Expectations) PathItem(io.swagger.v3.oas.models.PathItem) RefFormat(io.swagger.v3.parser.models.RefFormat) ResolverCache(io.swagger.v3.parser.ResolverCache) Test(org.testng.annotations.Test)

Example 13 with ResolverCache

use of io.swagger.v3.parser.ResolverCache in project swagger-parser by swagger-api.

the class ResolverCacheTest method testLoadExternalRef_NoDefinitionPath.

@Test
public void testLoadExternalRef_NoDefinitionPath() throws Exception {
    final RefFormat format = RefFormat.URL;
    final String ref = "http://my.company.com/path/to/file.json";
    final String contentsOfExternalFile = "really good json";
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolve(true);
    parseOptions.setValidateExternalRefs(true);
    new Expectations(DeserializationUtils.class) {

        {
            RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
            times = 1;
            result = contentsOfExternalFile;
            DeserializationUtils.deserializeIntoTree(contentsOfExternalFile, ref, parseOptions, (SwaggerParseResult) any);
            times = 1;
            result = new ObjectMapper().readTree("{\"type\":  \"string\"}");
        }
    };
    ResolverCache cache = new ResolverCache(openAPI, auths, "http://my.company.com/path/parent.json", new HashSet<>(), parseOptions);
    Schema firstActualResult = cache.loadRef(ref, RefFormat.URL, Schema.class);
    assertEquals(contentsOfExternalFile, cache.getExternalFileCache().get(ref));
    assertEquals(((Schema) cache.getResolutionCache().get(ref)).getType(), "string");
    assertEquals(firstActualResult.getType(), "string");
    // requesting the same ref a second time should not result in reading the external file again
    Schema secondActualResult = cache.loadRef(ref, format, Schema.class);
    assertEquals(secondActualResult.getType(), "string");
}
Also used : Expectations(mockit.Expectations) Schema(io.swagger.v3.oas.models.media.Schema) RefFormat(io.swagger.v3.parser.models.RefFormat) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) ResolverCache(io.swagger.v3.parser.ResolverCache) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 14 with ResolverCache

use of io.swagger.v3.parser.ResolverCache in project swagger-parser by swagger-api.

the class ResolverCacheTest method testLoadInternalDefinitionRefWithSpaces.

@Test
public void testLoadInternalDefinitionRefWithSpaces(@Injectable Schema mockedModel) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo bar", mockedModel));
    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Schema actualResult = cache.loadRef("#/components/schemas/foo bar", RefFormat.INTERNAL, Schema.class);
    assertEquals(actualResult, mockedModel);
}
Also used : Components(io.swagger.v3.oas.models.Components) Schema(io.swagger.v3.oas.models.media.Schema) ResolverCache(io.swagger.v3.parser.ResolverCache) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 15 with ResolverCache

use of io.swagger.v3.parser.ResolverCache in project swagger-parser by swagger-api.

the class ResolverCacheTest method testLoadInternalParameterRefWithSpaces.

@Test
public void testLoadInternalParameterRefWithSpaces(@Injectable Parameter mockedParameter) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addParameters("foo bar", mockedParameter));
    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Parameter actualResult = cache.loadRef("#/components/parameters/foo bar", RefFormat.INTERNAL, Parameter.class);
    assertEquals(actualResult, mockedParameter);
}
Also used : Components(io.swagger.v3.oas.models.Components) Parameter(io.swagger.v3.oas.models.parameters.Parameter) ResolverCache(io.swagger.v3.parser.ResolverCache) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)15 ResolverCache (io.swagger.v3.parser.ResolverCache)14 Components (io.swagger.v3.oas.models.Components)10 OpenAPI (io.swagger.v3.oas.models.OpenAPI)10 Schema (io.swagger.v3.oas.models.media.Schema)7 RefFormat (io.swagger.v3.parser.models.RefFormat)5 Expectations (mockit.Expectations)5 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Parameter (io.swagger.v3.oas.models.parameters.Parameter)2 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 NullNode (com.fasterxml.jackson.databind.node.NullNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ExternalDocumentation (io.swagger.v3.oas.models.ExternalDocumentation)1 PathItem (io.swagger.v3.oas.models.PathItem)1 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)1 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)1 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)1