use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class RemoteUrlTest method testAuthorizationHeader.
@Test
public void testAuthorizationHeader() throws Exception {
final String expectedBody = setupStub();
final String headerName = "Authorization";
final String headerValue = "foobar";
final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header");
final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
assertEquals(actualBody, expectedBody);
verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withHeader(headerName, equalTo(headerValue)));
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIV3Parser method readContents.
private SwaggerParseResult readContents(String swaggerAsString, List<AuthorizationValue> auth, ParseOptions options, String location) {
if (swaggerAsString == null || swaggerAsString.trim().isEmpty()) {
return SwaggerParseResult.ofError("Null or empty definition");
}
try {
final ObjectMapper mapper = getRightMapper(swaggerAsString);
JsonNode rootNode;
final SwaggerParseResult deserializationUtilsResult = new SwaggerParseResult();
if (options != null && options.isLegacyYamlDeserialization()) {
rootNode = mapper.readTree(swaggerAsString);
} else {
try {
rootNode = DeserializationUtils.deserializeIntoTree(swaggerAsString, location, options, deserializationUtilsResult);
} catch (Exception e) {
rootNode = mapper.readTree(swaggerAsString);
}
}
SwaggerParseResult result;
if (options != null) {
result = parseJsonNode(location, rootNode, options);
} else {
result = parseJsonNode(location, rootNode);
}
if (result.getOpenAPI() != null) {
result = resolve(result, auth, options, location);
}
if (deserializationUtilsResult.getMessages() != null) {
for (String s : deserializationUtilsResult.getMessages()) {
result.message(getParseErrorMessage(s, location));
}
}
return result;
} catch (JsonProcessingException e) {
LOGGER.warn("Exception while parsing:", e);
final String message = getParseErrorMessage(e.getOriginalMessage(), location);
return SwaggerParseResult.ofError(message);
} catch (Exception e) {
LOGGER.warn("Exception while parsing:", e);
final String message = getParseErrorMessage(e.getMessage(), location);
return SwaggerParseResult.ofError(message);
}
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIV3Parser method resolve.
private SwaggerParseResult resolve(SwaggerParseResult result, List<AuthorizationValue> auth, ParseOptions options, String location) {
try {
if (options != null) {
if (options.isResolve() || options.isResolveFully()) {
OpenAPIResolver resolver = new OpenAPIResolver(result.getOpenAPI(), emptyListIfNull(auth), location, null, options);
resolver.resolve(result);
if (options.isResolveFully()) {
new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
}
}
if (options.isFlatten()) {
final InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming(), options.isSkipMatches());
if (result.getOpenAPI() != null) {
inlineModelResolver.flatten(result.getOpenAPI());
}
}
}
} catch (Exception e) {
LOGGER.warn("Exception while resolving:", e);
// TODO verify if this change makes sense (adding resolve messages instead of replacing)
result.getMessages().add(e.getMessage());
// result.setMessages(Collections.singletonList(e.getMessage()));
}
return result;
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testIssue1170.
@Test
public void testIssue1170(@Injectable final List<AuthorizationValue> auths) {
String path = "/issue-1170/swagger.yaml";
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();
// Array schema with items $ref
Schema breedsListSchema = openAPI.getComponents().getSchemas().get("BreedsList");
Schema breedSchema = openAPI.getComponents().getSchemas().get("Breed");
assertNotNull(breedsListSchema);
assertNotNull(breedSchema);
assertTrue(breedsListSchema instanceof ArraySchema);
Schema breedPropertySchema = ((ArraySchema) breedsListSchema).getItems().getProperties().get("breed");
assertNotNull(breedPropertySchema);
// Verify items resolved fully
assertTrue(breedPropertySchema.get$ref() == null);
assertTrue(breedPropertySchema == breedSchema);
// Array schema with inline items object with $ref properties
Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring");
Schema colourSchema = openAPI.getComponents().getSchemas().get("Colour");
assertNotNull(petsListSchema);
assertNotNull(colouringsSchema);
assertNotNull(colourSchema);
assertTrue(petsListSchema instanceof ArraySchema);
Schema colouringPropertySchema = ((ArraySchema) petsListSchema).getItems().getProperties().get("colouring");
assertNotNull(colouringPropertySchema);
// Verify inline items resolved fully
assertTrue(colouringPropertySchema.get$ref() == null);
assertTrue(colouringPropertySchema == colouringsSchema);
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method testIssue1177.
@Test
public void testIssue1177(@Injectable final List<AuthorizationValue> auths) {
String path = "/issue-1177/swagger.yaml";
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();
// $ref response with $ref header
ApiResponse petsListApiResponse = openAPI.getPaths().get("/update-pets").getPost().getResponses().get("200");
assertNotNull(petsListApiResponse);
Header sessionIdHeader = petsListApiResponse.getHeaders().get("x-session-id");
assertNotNull(sessionIdHeader);
Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
assertNotNull(petsListSchema);
assertNotNull(openAPI.getComponents().getHeaders());
Header sessionIdHeaderComponent = openAPI.getComponents().getHeaders().get("x-session-id");
assertTrue(sessionIdHeader == sessionIdHeaderComponent);
assertTrue(petsListApiResponse.getContent().get("application/json").getSchema() == petsListSchema);
}
Aggregations