Java源码示例:io.swagger.v3.parser.util.RemoteUrl

示例1
/**
 * Parse and return a JsonNode representation of the input OAS document.
 * 
 * @param location the URL of the OAS document.
 * @param auths the list of authorization values to access the remote URL.
 * 
 * @throws java.lang.Exception if an error occurs while retrieving the OpenAPI document.
 * 
 * @return A JsonNode representation of the input OAS document.
 */
public static JsonNode readWithInfo(String location, List<AuthorizationValue> auths) throws Exception {
    String data;
    location = location.replaceAll("\\\\","/");
    if (location.toLowerCase(Locale.ROOT).startsWith("http")) {
        data = RemoteUrl.urlToString(location, auths);
    } else {
        final String fileScheme = "file:";
        Path path;
        if (location.toLowerCase(Locale.ROOT).startsWith(fileScheme)) {
            path = Paths.get(URI.create(location));
        } else {
            path = Paths.get(location);
        }
        if (Files.exists(path)) {
            data = FileUtils.readFileToString(path.toFile(), "UTF-8");
        } else {
            data = ClasspathHelper.loadFileFromClasspath(location);
        }
    }
    return getRightMapper(data).readTree(data);
}
 
示例2
@Test
public void testIssue213() throws Exception {
    new Expectations() {{
        RemoteUrl.urlToString("http://foo.bar.com/swagger.json", Arrays.asList(new AuthorizationValue[]{}));
        times = 1;
        result = spec;

        RemoteUrl.urlToString("http://foo.bar.com/path/samplePath.yaml", Arrays.asList(new AuthorizationValue[]{}));
        times = 1;
        result = samplePath;
    }};


    OpenAPI swagger = new OpenAPIV3Parser().read("http://foo.bar.com/swagger.json");
    assertNotNull(swagger);
    assertNotNull(swagger.getPaths().get("/samplePath"));

    assertNotNull(swagger.getPaths().get("/samplePath").getGet());
    assertNotNull(swagger.getPaths().get("/samplePath").getGet().getRequestBody());
    RequestBody body = swagger.getPaths().get("/samplePath").getGet().getRequestBody();
    assertNotNull(body.getContent().get("application/json").getSchema());
}
 
示例3
private OpenAPI deserializeSpec(String swaggerUrl) {
    OpenAPI exposedAPI = null;
    String location = config.getSwaggerUrl().replaceAll("\\\\", "/");
    try {
        String data = null;
        if (location.toLowerCase().startsWith("http")) {
            data = RemoteUrl.urlToString(location, null);
        } else {
            String fileScheme = "file:";
            Path path;
            if (location.toLowerCase().startsWith("file:")) {
                path = Paths.get(URI.create(location));
            } else {
                path = Paths.get(location);
            }

            if (Files.exists(path, new LinkOption[0])) {
                data = FileUtils.readFileToString(path.toFile(), "UTF-8");
            } else {
                data = ClasspathHelper.loadFileFromClasspath(location);
            }
            exposedAPI = getRightMapper(data).readValue(data, OpenAPI.class);
        }
    } catch (SSLHandshakeException e) {
        LOGGER.error("unable to read location `" + location + "` due to a SSL configuration error.  It is possible that the server SSL certificate is invalid, self-signed, or has an untrusted Certificate Authority.", e);
    } catch (Exception e1) {
        LOGGER.error("unable to read location `" + location + "`", e1);
    }
    return exposedAPI;
}
 
示例4
@Test
public void testRelativeRefIncludingUrlRef(@Injectable final Schema mockedModel)
		throws Exception {
	final RefFormat refFormat = RefFormat.RELATIVE;

	final String url = "https://my.example.remote.url.com/globals.yaml";

	final String expectedResult = "components:\n" +
			"  schemas:\n" +
			"    link-object:\n" +
			"      type: object\n" +
			"      additionalProperties:\n" +
			"        \"$ref\": \"#/components/schemas/rel-data\"\n" +
			"    rel-data:\n" +
			"      type: object\n" +
			"      required:\n" +
			"      - href\n" +
			"      properties:\n" +
			"        href:\n" +
			"          type: string\n" +
			"        note:\n" +
			"          type: string\n" +
			"    result:\n" +
			"      type: object\n" +
			"      properties:\n" +
			"        name:\n" +
			"          type: string\n" +
			"        _links:\n" +
			"          \"$ref\": \"#/components/schemas/link-object\"\n" +
			"";
	List<AuthorizationValue> auths = null;

	new Expectations() {{
		RemoteUrl.urlToString(url, auths);
		times = 1;
		result = expectedResult;
	}};

	OpenAPI mockedOpenAPI = new OpenAPI();
	mockedOpenAPI.setComponents(new Components());
	mockedOpenAPI.getComponents().setSchemas(new HashMap<>());
	ResolverCache mockedResolverCache = new ResolverCache(mockedOpenAPI, null, null);

	ExternalRefProcessor processor = new ExternalRefProcessor(mockedResolverCache, mockedOpenAPI);

	processor.processRefToExternalSchema("./relative-with-url/relative-with-url.yaml#/relative-with-url", refFormat);
	assertThat(((Schema) mockedOpenAPI.getComponents().getSchemas().get("relative-with-url").getProperties().get("Foo")).get$ref(),
			is("https://my.example.remote.url.com/globals.yaml#/components/schemas/link-object")
	);
	assertThat(mockedOpenAPI.getComponents().getSchemas().keySet().contains("link-object"), is(true));
	assertThat(mockedOpenAPI.getComponents().getSchemas().keySet().contains("rel-data"), is(true));
	// assert that ref is relative ref is resolved. and the file path is from root yaml file.
	assertThat(((Schema) mockedOpenAPI.getComponents().getSchemas().get("relative-with-url").getProperties().get("Bar")).get$ref(),
       is("./relative-with-url/relative-with-local.yaml#/relative-same-file")
   );
}