use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class SwaggerLegacyParser method read.
public ApiDeclaration read(String url, String resourcePath, Authentication authentication) {
MessageBuilder messageBuilder = new MessageBuilder();
SwaggerReader swaggerReader = new SwaggerReaderFactory(new SwaggerReaderConfiguration()).newReader();
JsonNode jsonNode = null;
try {
String resourceListingURL = getResourceListingURL(url, resourcePath);
jsonNode = swaggerReader.read(resourceListingURL, authentication, messageBuilder);
} catch (URISyntaxException e) {
messageBuilder.append(new Message("", e.getMessage(), Severity.ERROR));
}
validateMessageReport(messageBuilder);
ApiDeclarationMigrator apiDeclarationMigrator = new ApiDeclarationMigrator();
jsonNode = apiDeclarationMigrator.migrate(messageBuilder, jsonNode);
validateMessageReport(messageBuilder);
ApiDeclarationJsonValidator apiDeclarationJsonValidator = new ApiDeclarationJsonValidator();
apiDeclarationJsonValidator.validate(messageBuilder, jsonNode);
validateMessageReport(messageBuilder);
ApiDeclarationDeserializer apiDeclarationDeserializer = new ApiDeclarationDeserializer();
ApiDeclaration apiDeclaration = apiDeclarationDeserializer.deserialize(jsonNode, messageBuilder);
validateMessageReport(messageBuilder);
return apiDeclaration;
}
use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class ResourceListingConverterTest method convertResourceListingWithSubPath.
@Test
public void convertResourceListingWithSubPath() throws Exception {
ResourceListing rl = new ResourceListing();
rl.setApiVersion("2.11");
List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
ApiDeclaration api = new ApiDeclaration();
api.setBasePath("https://foo.com/baz/bar");
apis.add(api);
Swagger swagger = converter.convert(rl, apis);
assertTrue(swagger.getSchemes().size() == 1);
assertTrue(swagger.getSchemes().get(0).equals(Scheme.HTTPS));
assertTrue(swagger.getSwagger().equals("2.0"));
Info info = swagger.getInfo();
assertNotNull(info);
assertEquals(info.getVersion(), rl.getApiVersion());
assertEquals(swagger.getBasePath(), "/baz/bar");
assertEquals(swagger.getHost(), "foo.com");
}
use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class ApiDeclarationParser method read.
public ApiDeclaration read(String json, MessageBuilder messages) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setSerializationInclusion(Include.NON_NULL);
try {
Map<String, Object> m = mapper.readValue(json, Map.class);
ApiDeclaration api = new ApiDeclaration();
String apiVersion = readString(m.get("apiVersion"));
if (apiVersion != null) {
api.setApiVersion(apiVersion);
} else {
messages.append(new Message("ApiDeclaration.apiVersion", "apiVersion is missing", Severity.RECOMMENDED));
}
String swaggerVersion = readString(m.get("swaggerVersion"));
if (swaggerVersion != null) {
api.setSwaggerVersion(swaggerVersion);
} else {
messages.append(new Message("ApiDeclaration.swaggerVersion", "swaggerVersion is missing", Severity.ERROR));
}
String basePath = readString(m.get("basePath"));
if (basePath != null) {
api.setBasePath(basePath);
} else {
messages.append(new Message("ApiDeclaration.basePath", "basePath is missing", Severity.ERROR));
}
String resourcePath = readString(m.get("resourcePath"));
if (resourcePath != null) {
api.setResourcePath(resourcePath);
} else {
messages.append(new Message("ApiDeclaration.resourcePath", "resourcePath is missing", Severity.ERROR));
}
String produces = readString(m.get("produces"));
Object apis = m.get("apis");
if (apis != null) {
List<Api> o = readApis((List<Map<String, Object>>) apis, messages);
if (o.size() > 0) {
api.setApis(o);
}
}
Object models = m.get("models");
if (models != null) {
Map<String, Model> modelMap = readModels((Map<String, Object>) models, messages);
api.setModels(modelMap);
}
return api;
} catch (Exception e) {
messages.append(new Message("ApiDeclaration", "invalid json", Severity.ERROR));
return null;
}
}
use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class ApiDeclarationReader method main.
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
ApiDeclaration apiDeclaration = objectMapper.readValue(new URL("http://petstore.swagger.io/api/api-docs/store"), ApiDeclaration.class);
System.out.println(apiDeclaration);
}
use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class SwaggerCompatConverter method readDeclaration.
public ApiDeclaration readDeclaration(String input, MessageBuilder messages, List<AuthorizationValue> auths) {
ApiDeclaration output = null;
try {
JsonNode jsonNode = null;
if (input.startsWith("http")) {
String json = RemoteUrl.urlToString(input, auths);
jsonNode = Json.mapper().readTree(json);
} else {
final String fileScheme = "file:";
java.nio.file.Path path;
if (input.toLowerCase().startsWith(fileScheme)) {
path = Paths.get(URI.create(input));
} else {
path = Paths.get(input);
}
String json;
if (Files.exists(path)) {
json = FileUtils.readFileToString(path.toFile(), "UTF-8");
} else {
json = ClasspathHelper.loadFileFromClasspath(input);
}
jsonNode = Json.mapper().readTree(json);
}
// this should be moved to a json patch
if (jsonNode.isObject()) {
((ObjectNode) jsonNode).remove("authorizations");
}
ApiDeclarationMigrator migrator = new ApiDeclarationMigrator();
JsonNode transformed = migrator.migrate(messages, jsonNode);
output = Json.mapper().convertValue(transformed, ApiDeclaration.class);
} catch (java.lang.IllegalArgumentException e) {
return null;
} catch (Exception e) {
LOGGER.error("failed to read api declaration", e);
}
return output;
}
Aggregations