Search in sources :

Example 1 with ResolverCache

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

the class ComponentsProcessorTest method testComponentsSchemasProcessor.

@Test
public void testComponentsSchemasProcessor(@Injectable final Schema model1, @Injectable final Schema model2, @Injectable final ResolverCache cache) throws Exception {
    final OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo", model1));
    openAPI.getComponents().addSchemas("bar", model2);
    new Expectations() {

        {
            new SchemaProcessor(cache, openAPI);
            times = 1;
            result = schemaProcessor;
            schemaProcessor.processSchema((Schema) any);
            times = 2;
        }
    };
    new ComponentsProcessor(openAPI, cache).processComponents();
    new Verifications() {

        {
            schemaProcessor.processSchema(model1);
            schemaProcessor.processSchema(model2);
        }
    };
}
Also used : Components(io.swagger.v3.oas.models.Components) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 2 with ResolverCache

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

the class ResolverCacheTest method testLoadInternalDefinitionRef.

@Test
public void testLoadInternalDefinitionRef(@Injectable Schema mockedModel) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo", mockedModel));
    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Schema actualResult = cache.loadRef("#/components/schemas/foo", RefFormat.INTERNAL, Schema.class);
    assertEquals(actualResult, mockedModel);
    assertNull(cache.loadRef("#/components/schemas/bar", RefFormat.INTERNAL, Schema.class));
    assertNull(cache.loadRef("#/defs/bar", RefFormat.INTERNAL, Schema.class));
}
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 3 with ResolverCache

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

the class ResolverCacheTest method testMock.

@Test
public void testMock() 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(firstActualResult.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 4 with ResolverCache

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

the class ResolverCacheTest method testRenameCache.

@Test
public void testRenameCache() throws Exception {
    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    assertNull(cache.getRenamedRef("foo"));
    cache.putRenamedRef("foo", "bar");
    assertEquals(cache.getRenamedRef("foo"), "bar");
}
Also used : ResolverCache(io.swagger.v3.parser.ResolverCache) Test(org.testng.annotations.Test)

Example 5 with ResolverCache

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

the class ResolverCacheTest method testLoadInternalDefinitionRefWithEscapedCharacters.

@Test
public void testLoadInternalDefinitionRefWithEscapedCharacters(@Injectable Schema mockedModel) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo~bar/baz~1", mockedModel));
    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Schema actualResult = cache.loadRef("#/components/schemas/foo~0bar~1baz~01", 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)

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