use of io.swagger.v3.parser.core.models.ParseOptions in project swagger-parser by swagger-api.
the class V2ConverterTest method testInlineDefinitionProperty.
@Test()
public void testInlineDefinitionProperty() throws Exception {
SwaggerConverter converter = new SwaggerConverter();
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
parseOptions.setFlatten(true);
SwaggerParseResult result = converter.readLocation("src/test/resources/issue-1359.yaml", null, parseOptions);
OpenAPI oas = result.getOpenAPI();
assertNotNull(oas);
Schema pet = oas.getComponents().getSchemas().get("Pet");
Schema property = (Schema) pet.getProperties().get("categoryInline");
assertEquals("#/components/schemas/Pet_categoryInline", property.get$ref());
Schema petCategoryInline = oas.getComponents().getSchemas().get("Pet_categoryInline");
assertNotNull(petCategoryInline);
}
use of io.swagger.v3.parser.core.models.ParseOptions 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.ParseOptions in project swagger-parser by swagger-api.
the class OpenAPIV3Parser method readContents.
public SwaggerParseResult readContents(String yaml) {
final ParseOptions options = new ParseOptions();
options.setResolve(true);
return readContents(yaml, null, options);
}
use of io.swagger.v3.parser.core.models.ParseOptions 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.ParseOptions 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);
}
Aggregations