diff --git a/libraries-data-2/pom.xml b/libraries-data-2/pom.xml index f673ebd470..655bad2e2b 100644 --- a/libraries-data-2/pom.xml +++ b/libraries-data-2/pom.xml @@ -134,6 +134,11 @@ ${byte-buddy.version} test + + io.swagger.parser.v3 + swagger-parser + ${swagger-parser.version} + @@ -171,6 +176,7 @@ 1.1.0 3.0.0 2.8.4 + 2.1.13 \ No newline at end of file diff --git a/libraries-data-2/src/main/java/com/baeldung/swaggerparser/SwaggerParser.java b/libraries-data-2/src/main/java/com/baeldung/swaggerparser/SwaggerParser.java new file mode 100644 index 0000000000..d1f04e680a --- /dev/null +++ b/libraries-data-2/src/main/java/com/baeldung/swaggerparser/SwaggerParser.java @@ -0,0 +1,92 @@ +package com.baeldung.swaggerparser; + +import java.util.List; + +import io.swagger.parser.OpenAPIParser; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.Paths; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.responses.ApiResponses; +import io.swagger.v3.oas.models.servers.Server; +import io.swagger.v3.parser.core.models.SwaggerParseResult; + +public class SwaggerParser { + + private static String OPENAPI_SPECIFICATION_STRING = "{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"User APIs\",\"version\":\"1.0\"},\"servers\":[{\"url\":\"https://jsonplaceholder.typicode.com\",\"description\":\"Json Place Holder Service\"}],\"paths\":{\"/users/{id}\":{\"parameters\":[{\"schema\":{\"type\":\"integer\"},\"name\":\"id\",\"in\":\"path\",\"required\":true}],\"get\":{\"summary\":\"Fetch user by ID\",\"tags\":[\"User\"],\"responses\":{\"200\":{\"description\":\"OK\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}}}}}},\"operationId\":\"get-users-user_id\",\"description\":\"Retrieve a specific user by ID\"}}}}"; + + public static void main(String[] args) { + parseYAMLFile(); + parseJSONFile(); + parseString(); + } + + private static void parseString() { + SwaggerParseResult result = new OpenAPIParser().readContents(OPENAPI_SPECIFICATION_STRING, null, null); + + OpenAPI openAPI = result.getOpenAPI(); + + if (openAPI != null) { + printData(openAPI); + } + } + + private static void parseJSONFile() { + SwaggerParseResult result = new OpenAPIParser().readLocation("sample.yml", null, null); + + OpenAPI openAPI = result.getOpenAPI(); + + if (openAPI != null) { + printData(openAPI); + } + } + + private static void parseYAMLFile() { + SwaggerParseResult result = new OpenAPIParser().readLocation("sample.json", null, null); + + OpenAPI openAPI = result.getOpenAPI(); + + if (openAPI != null) { + printData(openAPI); + } + } + + private static void printData(OpenAPI openAPI) { + System.out.println(openAPI.getSpecVersion()); + + Info info = openAPI.getInfo(); + System.out.println(info.getTitle()); + System.out.println(info.getVersion()); + + List servers = openAPI.getServers(); + for (Server server : servers) { + System.out.println(server.getUrl()); + System.out.println(server.getDescription()); + } + + Paths paths = openAPI.getPaths(); + paths.entrySet() + .forEach(pathEntry -> { + System.out.println(pathEntry.getKey()); + + PathItem path = pathEntry.getValue(); + System.out.println(path.getGet() + .getSummary()); + System.out.println(path.getGet() + .getDescription()); + System.out.println(path.getGet() + .getOperationId()); + + ApiResponses responses = path.getGet() + .getResponses(); + responses.entrySet() + .forEach(responseEntry -> { + System.out.println(responseEntry.getKey()); + + ApiResponse response = responseEntry.getValue(); + System.out.println(response.getDescription()); + }); + }); + } +} diff --git a/libraries-data-2/src/main/resources/sample.json b/libraries-data-2/src/main/resources/sample.json new file mode 100644 index 0000000000..8b59bbb42a --- /dev/null +++ b/libraries-data-2/src/main/resources/sample.json @@ -0,0 +1,55 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "User APIs", + "version": "1.0" + }, + "servers": [ + { + "url": "https://jsonplaceholder.typicode.com", + "description": "Json Place Holder Service" + } + ], + "paths": { + "/users/{id}": { + "parameters": [ + { + "schema": { + "type": "integer" + }, + "name": "id", + "in": "path", + "required": true + } + ], + "get": { + "summary": "Fetch user by ID", + "tags": [ + "User" + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + } + } + } + } + } + } + }, + "operationId": "get-users-user_id", + "description": "Retrieve a specific user by ID" + } + } + } +} \ No newline at end of file diff --git a/libraries-data-2/src/main/resources/sample.yml b/libraries-data-2/src/main/resources/sample.yml new file mode 100644 index 0000000000..ddfa064393 --- /dev/null +++ b/libraries-data-2/src/main/resources/sample.yml @@ -0,0 +1,33 @@ +openapi: 3.0.0 +info: + title: User APIs + version: '1.0' +servers: + - url: https://jsonplaceholder.typicode.com + description: Json Place Holder Service +paths: + /users/{id}: + parameters: + - schema: + type: integer + name: id + in: path + required: true + get: + summary: Fetch user by ID + tags: + - User + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + id: + type: integer + name: + type: string + operationId: get-users-user_id + description: Retrieve a specific user by ID \ No newline at end of file diff --git a/libraries-data-2/src/test/java/com/baeldung/swaggerparser/SwaggerParserUnitTest.java b/libraries-data-2/src/test/java/com/baeldung/swaggerparser/SwaggerParserUnitTest.java new file mode 100644 index 0000000000..615bd917cb --- /dev/null +++ b/libraries-data-2/src/test/java/com/baeldung/swaggerparser/SwaggerParserUnitTest.java @@ -0,0 +1,65 @@ +package com.baeldung.swaggerparser; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import io.swagger.parser.OpenAPIParser; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.parser.core.models.SwaggerParseResult; + +public class SwaggerParserUnitTest { + + private static String OPENAPI_SPECIFICATION_STRING = "{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"User APIs\",\"version\":\"1.0\"},\"servers\":[{\"url\":\"https://jsonplaceholder.typicode.com\",\"description\":\"Json Place Holder Service\"}],\"paths\":{\"/users/{id}\":{\"parameters\":[{\"schema\":{\"type\":\"integer\"},\"name\":\"id\",\"in\":\"path\",\"required\":true}],\"get\":{\"summary\":\"Fetch user by ID\",\"tags\":[\"User\"],\"responses\":{\"200\":{\"description\":\"OK\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}}}}}},\"operationId\":\"get-users-user_id\",\"description\":\"Retrieve a specific user by ID\"}}}}"; + + @Test + public void givenAValidOpenAPIYAMLFile_whenParsing_thenCheckIfParsed() { + SwaggerParseResult result = new OpenAPIParser().readLocation("valid-openapi-sample.yml", null, null); + + OpenAPI openAPI = result.getOpenAPI(); + + assertNotNull(openAPI); + } + + @Test + public void givenAValidOpenAPIYAMLFile_whenParsing_thenCheckIfNotErrors() { + SwaggerParseResult result = new OpenAPIParser().readLocation("valid-openapi-sample.yml", null, null); + + List messages = result.getMessages(); + + assertTrue(messages.isEmpty()); + } + + @Test + public void givenAnInvalidOpenAPIYAMLFile_whenParsing_thenCheckIfErrors() { + SwaggerParseResult result = new OpenAPIParser().readLocation("invalid-openapi-sample.yml", null, null); + + List messages = result.getMessages(); + + assertFalse(messages.isEmpty()); + } + + @Test + public void givenAValidOpenAPISpecificationString_whenParsing_thenCheckIfParsed() { + SwaggerParseResult result = new OpenAPIParser().readContents(OPENAPI_SPECIFICATION_STRING, null, null); + + OpenAPI openAPI = result.getOpenAPI(); + + assertNotNull(openAPI); + } + + @Test + public void givenAValidOpenAPISpecificationString_whenParsing_thenCheckIfNotErrors() { + SwaggerParseResult result = new OpenAPIParser().readLocation(OPENAPI_SPECIFICATION_STRING, null, null); + + List messages = result.getMessages(); + + assertNull(messages); + } + +} diff --git a/libraries-data-2/src/test/resources/invalid-openapi-sample.yml b/libraries-data-2/src/test/resources/invalid-openapi-sample.yml new file mode 100644 index 0000000000..eaac64c9a9 --- /dev/null +++ b/libraries-data-2/src/test/resources/invalid-openapi-sample.yml @@ -0,0 +1,32 @@ +openapi: 3.0.0 +info: + title: User APIs + version: '1.0' +servers: + - description: Json Place Holder Service +paths: + /users/{id}: + parameters: + - schema: + type: integer + name: id + in: path + required: true + get: + summary: Fetch user by ID + tags: + - User + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + id: + type: integer + name: + type: string + operationId: get-users-user_id + description: Retrieve a specific user by ID \ No newline at end of file diff --git a/libraries-data-2/src/test/resources/valid-openapi-sample.yml b/libraries-data-2/src/test/resources/valid-openapi-sample.yml new file mode 100644 index 0000000000..ddfa064393 --- /dev/null +++ b/libraries-data-2/src/test/resources/valid-openapi-sample.yml @@ -0,0 +1,33 @@ +openapi: 3.0.0 +info: + title: User APIs + version: '1.0' +servers: + - url: https://jsonplaceholder.typicode.com + description: Json Place Holder Service +paths: + /users/{id}: + parameters: + - schema: + type: integer + name: id + in: path + required: true + get: + summary: Fetch user by ID + tags: + - User + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + id: + type: integer + name: + type: string + operationId: get-users-user_id + description: Retrieve a specific user by ID \ No newline at end of file