use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class OperationConverterTest method convertOperation1.
@Test
public void convertOperation1() throws Exception {
io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation();
operation.setMethod(Method.GET);
operation.setSummary("the summary");
operation.setNotes("the notes");
operation.setNickname("getFun");
List<String> produces = new ArrayList<String>();
produces.add("application/json");
operation.setProduces(produces);
// response type
operation.setRef("Cat");
// response messages
List<ResponseMessage> responses = new ArrayList<ResponseMessage>();
ResponseMessage message400 = new ResponseMessage();
message400.setCode(400);
message400.setMessage("got a 400");
responses.add(message400);
operation.setResponseMessages(responses);
// parameters
io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
param.setParamType(ParamType.QUERY);
param.setDescription("a string query param");
param.setRequired(false);
param.setAllowMultiple(false);
param.setType("string");
List<io.swagger.models.apideclaration.Parameter> parameters = new ArrayList<io.swagger.models.apideclaration.Parameter>();
parameters.add(param);
operation.setParameters(parameters);
Operation converted = converter.convertOperation("tag", operation, new ApiDeclaration());
assertTrue(converted.getTags().size() == 1);
assertEquals(converted.getTags().get(0), "tag");
assertEquals(operation.getSummary(), converted.getSummary());
assertEquals(operation.getNotes(), converted.getDescription());
assertEquals(operation.getNickname(), converted.getOperationId());
assertTrue(converted.getProduces().size() == 1);
assertEquals(converted.getProduces().get(0), "application/json");
assertTrue(converted.getParameters().size() == 1);
assertTrue(converted.getResponses().size() == 2);
Response response = converted.getResponses().get("200");
assertNotNull(response);
assertEquals(response.getDescription(), "success");
Model schema = response.getResponseSchema();
assertNotNull(schema);
assertTrue(schema.getClass().equals(RefModel.class));
RefModel ref = (RefModel) schema;
assertEquals(ref.getSimpleRef(), "Cat");
}
use of io.swagger.models.apideclaration.ApiDeclaration 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.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class ResourceListingConverterTest method convertResourceListingWithRootPath.
@Test
public void convertResourceListingWithRootPath() throws Exception {
ResourceListing rl = new ResourceListing();
rl.setApiVersion("2.11");
List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
ApiDeclaration api = new ApiDeclaration();
api.setBasePath("http://foo.com");
apis.add(api);
Swagger swagger = converter.convert(rl, apis);
assertTrue(swagger.getSchemes().size() == 1);
assertTrue(swagger.getSwagger().equals("2.0"));
Info info = swagger.getInfo();
assertNotNull(info);
assertEquals(info.getVersion(), rl.getApiVersion());
assertEquals(swagger.getBasePath(), "/");
assertEquals(swagger.getHost(), "foo.com");
}
use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class ResourceListingReader method main.
public static void main(String[] args) throws IOException, URISyntaxException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
String baseUrl = "http://petstore.swagger.io/api/api-docs";
ResourceListing resourceListing = objectMapper.readValue(new URL(baseUrl), ResourceListing.class);
Map<String, ApiDeclaration> apiDeclarations = new HashMap<>();
List<ApiListingReference> apis = resourceListing.getApis();
if (apis != null) {
for (ApiListingReference api : apis) {
URL apiUrl;
URI uri = new URI(api.getPath());
if (uri.isAbsolute()) {
apiUrl = uri.toURL();
} else {
apiUrl = new URL(baseUrl + api.getPath());
}
apiDeclarations.put(api.getPath(), objectMapper.readValue(apiUrl, ApiDeclaration.class));
}
}
System.out.println("---=== Resource Listing (" + baseUrl + ") ==--");
System.out.println(resourceListing);
for (Map.Entry<String, ApiDeclaration> apiDeclarationEntry : apiDeclarations.entrySet()) {
System.out.println("---=== API Declaration (" + apiDeclarationEntry.getKey() + ") ==--");
System.out.println(apiDeclarationEntry.getValue());
}
}
use of io.swagger.models.apideclaration.ApiDeclaration in project swagger-parser by swagger-api.
the class SwaggerCompatConverter method read.
@Override
public Swagger read(String input, List<AuthorizationValue> auths) throws IOException {
Swagger output = null;
MessageBuilder migrationMessages = new MessageBuilder();
SwaggerLegacyParser swaggerParser = new SwaggerLegacyParser();
ResourceListing resourceListing = null;
resourceListing = readResourceListing(input, migrationMessages, auths);
List<ApiDeclaration> apis = new ArrayList<ApiDeclaration>();
if (resourceListing != null) {
List<ApiListingReference> refs = resourceListing.getApis();
boolean readAsSingleFile = false;
if (refs != null) {
for (ApiListingReference ref : refs) {
ApiDeclaration apiDeclaration = null;
JsonNode node = ref.getExtraFields();
JsonNode operations = node.get("operations");
if (operations != null) {
if (!readAsSingleFile) {
// this is a single-file swagger definition
apiDeclaration = readDeclaration(input, migrationMessages, auths);
// avoid doing this again
readAsSingleFile = true;
}
} else {
String location = null;
if (input.startsWith("http")) {
// look up as url
String pathLocation = ref.getPath();
if (pathLocation.startsWith("http")) {
// use as absolute url
location = pathLocation;
} else {
if (pathLocation.startsWith("/")) {
// handle 1.1 specs
if (resourceListing.getSwaggerVersion().equals(SwaggerVersion.V1_1) && resourceListing.getExtraFields().get("basePath") != null) {
String basePath = resourceListing.getExtraFields().get("basePath").textValue();
location = basePath + pathLocation;
} else {
location = input + pathLocation;
}
} else {
location = input + "/" + pathLocation;
}
}
} else {
// file system
File fileLocation = new File(input);
if (ref.getPath().startsWith("/")) {
location = fileLocation.getParent() + ref.getPath();
} else {
location = fileLocation.getParent() + File.separator + ref.getPath();
}
}
if (location.indexOf(".{format}") != -1) {
location = location.replaceAll("\\.\\{format\\}", ".json");
}
apiDeclaration = readDeclaration(location, migrationMessages, auths);
}
if (apiDeclaration != null) {
apis.add(apiDeclaration);
}
}
}
output = convert(resourceListing, apis);
}
return output;
}
Aggregations