Search in sources :

Example 16 with RefFormat

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

the class ExternalRefProcessorTest method testProcessRefToExternalDefinition_NoNameConflict.

@Test
public void testProcessRefToExternalDefinition_NoNameConflict(@Injectable final Schema mockedModel) throws Exception {
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final RefFormat refFormat = RefFormat.URL;
    new StrictExpectations() {

        {
            cache.getRenamedRef(ref);
            times = 1;
            result = null;
            cache.loadRef(ref, refFormat, Schema.class);
            times = 1;
            result = mockedModel;
            openAPI.getComponents();
            times = 1;
            result = new Components();
            openAPI.getComponents().getSchemas();
            times = 1;
            result = null;
            cache.putRenamedRef(ref, "bar");
            openAPI.getComponents().addSchemas("bar", mockedModel);
            times = 1;
            cache.addReferencedKey("bar");
            times = 1;
            result = null;
        }
    };
    String newRef = new ExternalRefProcessor(cache, openAPI).processRefToExternalSchema(ref, refFormat);
    assertEquals(newRef, "bar");
}
Also used : Components(io.swagger.v3.oas.models.Components) StrictExpectations(mockit.StrictExpectations) RefFormat(io.swagger.v3.parser.models.RefFormat) Test(org.testng.annotations.Test)

Example 17 with RefFormat

use of io.swagger.v3.parser.models.RefFormat 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 18 with RefFormat

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

the class ResolverCacheTest method testLoadExternalRefResponseWithNoContent.

@Test
public void testLoadExternalRefResponseWithNoContent() throws Exception {
    final RefFormat format = RefFormat.URL;
    final String ref = "http://my.company.com/path/to/main.yaml";
    final String contentsOfExternalFile = "openapi: 3.0.0\n" + "\n" + "info:\n" + "  version: 1.0.0\n" + "  title: Response include test case child\n" + "\n" + "components:\n" + "  responses:\n" + "    200:\n" + "      description: Success\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");
    ApiResponse response = cache.loadRef(ref + "#/components/responses/200", RefFormat.URL, ApiResponse.class);
    assertNotNull(response);
    assertEquals(response.getDescription(), "Success");
    assertNull(response.getContent());
}
Also used : Expectations(mockit.Expectations) RefFormat(io.swagger.v3.parser.models.RefFormat) ResolverCache(io.swagger.v3.parser.ResolverCache) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 19 with RefFormat

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

the class HeaderProcessor method processHeader.

public void processHeader(Header header) {
    if (header.get$ref() != null) {
        RefFormat refFormat = computeRefFormat(header.get$ref());
        String $ref = header.get$ref();
        if (isAnExternalRefFormat(refFormat)) {
            final String newRef = externalRefProcessor.processRefToExternalHeader($ref, refFormat);
            if (newRef != null) {
                header.set$ref(newRef);
            }
        }
    }
    if (header.getSchema() != null) {
        schemaProcessor.processSchema(header.getSchema());
    }
    if (header.getExamples() != null) {
        if (header.getExamples() != null) {
            Map<String, Example> examples = header.getExamples();
            for (String key : examples.keySet()) {
                exampleProcessor.processExample(header.getExamples().get(key));
            }
        }
    }
    Schema schema = null;
    if (header.getContent() != null) {
        Map<String, MediaType> content = header.getContent();
        for (String mediaName : content.keySet()) {
            MediaType mediaType = content.get(mediaName);
            if (mediaType.getSchema() != null) {
                schema = mediaType.getSchema();
                if (schema != null) {
                    schemaProcessor.processSchema(schema);
                }
            }
        }
    }
}
Also used : Example(io.swagger.v3.oas.models.examples.Example) Schema(io.swagger.v3.oas.models.media.Schema) RefFormat(io.swagger.v3.parser.models.RefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) MediaType(io.swagger.v3.oas.models.media.MediaType)

Example 20 with RefFormat

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

the class ExternalRefProcessor method processRefToExternalLink.

public String processRefToExternalLink(String $ref, RefFormat refFormat) {
    String renamedRef = cache.getRenamedRef($ref);
    if (renamedRef != null) {
        return renamedRef;
    }
    final Link link = cache.loadRef($ref, refFormat, Link.class);
    if (link == null) {
        // stop!  There's a problem.  retain the original ref
        LOGGER.warn("unable to load model reference from `" + $ref + "`.  It may not be available " + "or the reference isn't a valid model schema");
        return $ref;
    }
    String newRef;
    if (openAPI.getComponents() == null) {
        openAPI.setComponents(new Components());
    }
    Map<String, Link> links = openAPI.getComponents().getLinks();
    if (links == null) {
        links = new LinkedHashMap<>();
    }
    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
    Link existingLink = links.get(possiblyConflictingDefinitionName);
    if (existingLink != null) {
        LOGGER.debug("A model for " + existingLink + " already exists");
        if (existingLink.get$ref() != null) {
            // use the new model
            existingLink = null;
        }
    }
    newRef = possiblyConflictingDefinitionName;
    cache.putRenamedRef($ref, newRef);
    if (existingLink == null) {
        // don't overwrite existing model reference
        openAPI.getComponents().addLinks(newRef, link);
        cache.addReferencedKey(newRef);
        String file = $ref.split("#/")[0];
        if (link.get$ref() != null) {
            RefFormat format = computeRefFormat(link.get$ref());
            if (isAnExternalRefFormat(format)) {
                link.set$ref(processRefToExternalLink(link.get$ref(), format));
            } else {
                processRefToExternalLink(file + link.get$ref(), RefFormat.RELATIVE);
            }
        }
    }
    return newRef;
}
Also used : Components(io.swagger.v3.oas.models.Components) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) Link(io.swagger.v3.oas.models.links.Link)

Aggregations

RefFormat (io.swagger.v3.parser.models.RefFormat)38 RefUtils.computeRefFormat (io.swagger.v3.parser.util.RefUtils.computeRefFormat)30 RefUtils.isAnExternalRefFormat (io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat)29 Components (io.swagger.v3.oas.models.Components)12 Schema (io.swagger.v3.oas.models.media.Schema)8 Test (org.testng.annotations.Test)7 ResolverCache (io.swagger.v3.parser.ResolverCache)6 Expectations (mockit.Expectations)6 Example (io.swagger.v3.oas.models.examples.Example)5 PathItem (io.swagger.v3.oas.models.PathItem)4 MediaType (io.swagger.v3.oas.models.media.MediaType)4 Parameter (io.swagger.v3.oas.models.parameters.Parameter)4 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)4 OpenAPI (io.swagger.v3.oas.models.OpenAPI)3 Callback (io.swagger.v3.oas.models.callbacks.Callback)3 Header (io.swagger.v3.oas.models.headers.Header)3 Link (io.swagger.v3.oas.models.links.Link)3 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)3 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)3 Map (java.util.Map)3