use of io.swagger.parser.SwaggerCompatConverter in project swagger-parser by swagger-api.
the class OperationConverterTest method testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration.
@Test
public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration() throws Exception {
Set<String> expectedConsumes = new HashSet<>(Arrays.asList("application/json", "application/xml"));
Set<String> expectedProduces = new HashSet<>(Arrays.asList("text/plain"));
final ApiDeclaration apiDeclaration = new ApiDeclaration();
apiDeclaration.setConsumes(new ArrayList<>(expectedConsumes));
apiDeclaration.setProduces(new ArrayList<>(expectedProduces));
io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation();
operation.setMethod(Method.GET);
final SwaggerCompatConverter swaggerCompatConverter = new SwaggerCompatConverter();
Operation converted = swaggerCompatConverter.convertOperation("tag", operation, apiDeclaration);
assertSetsAreEqual(expectedConsumes, converted.getConsumes());
assertSetsAreEqual(expectedProduces, converted.getProduces());
}
use of io.swagger.parser.SwaggerCompatConverter in project swagger-parser by swagger-api.
the class SwaggerCompatConverterTest method loadsSpecFromClasspath.
@Test
public void loadsSpecFromClasspath() throws IOException {
SwaggerCompatConverter converter = new SwaggerCompatConverter();
Swagger result = converter.read("/specs/v1_2/singleFile.json");
assertTrue(result != null, "Didn't load spec from classpath");
}
use of io.swagger.parser.SwaggerCompatConverter in project swagger-parser by swagger-api.
the class SwaggerCompatConverterTest method testIssue_641.
@Test
public void testIssue_641() throws IOException {
SwaggerCompatConverter converter = new SwaggerCompatConverter();
Swagger swagger2 = converter.read("specs/v1_2/issue-641.json");
assertNotNull(swagger2);
BodyParameter bodyParameter = (BodyParameter) swagger2.getPaths().get("/user/createWithArray").getPost().getParameters().get(0);
assertEquals(((RefProperty) ((ArrayModel) bodyParameter.getSchema()).getItems()).get$ref(), "#/definitions/User");
}
use of io.swagger.parser.SwaggerCompatConverter in project swagger-parser by swagger-api.
the class SwaggerCompatConverterTest method failsOnNonExistentSpec.
@Test
public void failsOnNonExistentSpec() throws IOException {
SwaggerCompatConverter converter = new SwaggerCompatConverter();
Swagger result = converter.read("specs/v1_2/not-exists.json");
assertTrue(result == null);
}
use of io.swagger.parser.SwaggerCompatConverter in project gravitee-management-rest-api by gravitee-io.
the class SwaggerServiceImpl method prepare.
public NewApiEntity prepare(ImportSwaggerDescriptorEntity swaggerDescriptor) {
Swagger swagger = null;
switch(swaggerDescriptor.getVersion()) {
case VERSION_1_0:
try {
logger.info("Loading an old Swagger descriptor from {}", swaggerDescriptor.getPayload());
// For spec < 2.0, only read by url is possible
swagger = new SwaggerCompatConverter().read(swaggerDescriptor.getPayload());
} catch (IOException ioe) {
logger.error("Can not read old Swagger specification", ioe);
throw new SwaggerDescriptorException();
}
case VERSION_2_0:
if (swaggerDescriptor.getType() == ImportSwaggerDescriptorEntity.Type.INLINE) {
logger.info("Loading an inline Swagger descriptor");
swagger = new SwaggerParser().parse(swaggerDescriptor.getPayload());
} else if (swaggerDescriptor.getType() == ImportSwaggerDescriptorEntity.Type.URL) {
logger.info("Loading a Swagger descriptor from URL: ", swaggerDescriptor.getPayload());
swagger = new SwaggerParser().read(swaggerDescriptor.getPayload());
}
}
if (swagger == null) {
throw new SwaggerDescriptorException();
}
NewApiEntity apiEntity = new NewApiEntity();
apiEntity.setName(swagger.getInfo().getTitle());
apiEntity.setDescription(swagger.getInfo().getDescription());
apiEntity.setVersion(swagger.getInfo().getVersion());
String scheme = (swagger.getSchemes() == null || swagger.getSchemes().isEmpty()) ? defaultScheme : swagger.getSchemes().iterator().next().toValue();
apiEntity.setEndpoint(scheme + "://" + swagger.getHost() + swagger.getBasePath());
apiEntity.setPaths(new ArrayList<>(swagger.getPaths().keySet().stream().map(path -> path.replaceAll("\\{(.[^/]*)\\}", ":$1")).collect(Collectors.toList())));
return apiEntity;
}
Aggregations