From 0b155275a3cbbe8c37f7bb4d542be0903560e3c4 Mon Sep 17 00:00:00 2001 From: msendetckii Date: Mon, 25 May 2020 13:10:30 +0200 Subject: [PATCH] Added an OpenAPI-based API definition. --- application/build.gradle | 18 ++- .../example/demo/rest/GreetingController.java | 6 +- application/src/main/resources/api.yaml | 146 ++++++++++++++++++ build.gradle | 1 + .../java/com/{ => example/demo}/Greeting.java | 4 +- .../src/test/java/com/GreetingTest.java | 1 + 6 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 application/src/main/resources/api.yaml rename domain-entities/src/main/java/com/{ => example/demo}/Greeting.java (83%) diff --git a/application/build.gradle b/application/build.gradle index 9b8c6f4..8bc9529 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -1,5 +1,6 @@ plugins { id 'org.springframework.boot' + id 'org.openapi.generator' version '4.3.1' } configurations { @@ -40,4 +41,19 @@ dependencies { test { useJUnitPlatform() -} \ No newline at end of file +} + +// For Maven see: +// https://reflectoring.io/spring-boot-openapi/ +// https://github.com/thombergs/code-examples/tree/master/spring-boot/spring-boot-openapi + +openApiGenerate { + generatorName = "spring" + inputSpec = "$projectDir/src/main/resources/api.yaml" + outputDir = "$buildDir/generated/api" + apiPackage = "com.example.demo.api" + invokerPackage = "com.example.demo.invoker" + modelPackage = "com.example.demo.model" +} + +compileJava.dependsOn tasks.openApiGenerate diff --git a/application/src/main/java/com/example/demo/rest/GreetingController.java b/application/src/main/java/com/example/demo/rest/GreetingController.java index 4b5902b..613e4b5 100644 --- a/application/src/main/java/com/example/demo/rest/GreetingController.java +++ b/application/src/main/java/com/example/demo/rest/GreetingController.java @@ -1,8 +1,6 @@ package com.example.demo.rest; -import java.util.concurrent.atomic.AtomicLong; - -import com.Greeting; +import com.example.demo.Greeting; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -10,7 +8,7 @@ @RestController public class GreetingController { - @GetMapping("/") + @GetMapping("/greeting") public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) { return Greeting.builder() .value("Hello World") diff --git a/application/src/main/resources/api.yaml b/application/src/main/resources/api.yaml new file mode 100644 index 0000000..35870b0 --- /dev/null +++ b/application/src/main/resources/api.yaml @@ -0,0 +1,146 @@ +openapi: 3.0.2 +info: + title: Reflectoring + description: "???" + termsOfService: http://swagger.io/terms/ + contact: + email: maksim.sendetckii@de.sii.group + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0-SNAPSHOT +externalDocs: + description: Find out more about Reflectoring + url: https://reflectoring.io/about/ +servers: + - url: https://reflectoring.swagger.io/v2 +tags: + - name: user + description: Operations about user + externalDocs: + description: Find out more about our store + url: http://swagger.io +paths: + /user: + post: + tags: + - user + summary: Create user + description: Create user functionality + operationId: createUser + requestBody: + description: Created user object + content: + '*/*': + schema: + $ref: '#/components/schemas/User' + required: true + responses: + default: + description: successful operation + content: {} + x-codegen-request-body-name: body + /user/{username}: + get: + tags: + - user + summary: Get user by user name + operationId: getUserByName + parameters: + - name: username + in: path + description: 'The name that needs to be fetched. Use user1 for testing. ' + required: true + schema: + type: string + responses: + 200: + description: successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/User' + 404: + description: User not found + content: {} + put: + tags: + - user + summary: Updated user + description: This can only be done by the logged in user. + operationId: updateUser + parameters: + - name: username + in: path + description: name that need to be updated + required: true + schema: + type: string + requestBody: + description: Updated user object + content: + '*/*': + schema: + $ref: '#/components/schemas/User' + required: true + responses: + 200: + description: successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/User' + 400: + description: Invalid user supplied + content: {} + 404: + description: User not found + content: {} + x-codegen-request-body-name: body + delete: + tags: + - user + summary: Delete user + description: This can only be done by the logged in user. + operationId: deleteUser + parameters: + - name: username + in: path + description: The name that needs to be deleted + required: true + schema: + type: string + responses: + 201: + description: operation successful + content: {} + 400: + description: Invalid username supplied + content: {} + 404: + description: User not found + content: {} +components: + schemas: + User: + type: object + properties: + id: + type: integer + format: int64 + username: + type: string + firstName: + type: string + lastName: + type: string + email: + type: string + password: + type: string + phone: + type: string + userStatus: + type: integer + description: User Status + format: int32 diff --git a/build.gradle b/build.gradle index a496929..f0c8c70 100644 --- a/build.gradle +++ b/build.gradle @@ -35,6 +35,7 @@ subprojects { } } + dependencies { compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/domain-entities/src/main/java/com/Greeting.java b/domain-entities/src/main/java/com/example/demo/Greeting.java similarity index 83% rename from domain-entities/src/main/java/com/Greeting.java rename to domain-entities/src/main/java/com/example/demo/Greeting.java index 7f04e3d..64b4118 100644 --- a/domain-entities/src/main/java/com/Greeting.java +++ b/domain-entities/src/main/java/com/example/demo/Greeting.java @@ -1,4 +1,4 @@ -package com; +package com.example.demo; import lombok.Builder; import lombok.NonNull; @@ -9,4 +9,4 @@ public class Greeting { @NonNull String value; -} \ No newline at end of file +} diff --git a/domain-entities/src/test/java/com/GreetingTest.java b/domain-entities/src/test/java/com/GreetingTest.java index 0953083..370256a 100644 --- a/domain-entities/src/test/java/com/GreetingTest.java +++ b/domain-entities/src/test/java/com/GreetingTest.java @@ -1,5 +1,6 @@ package com; +import com.example.demo.Greeting; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat;