diff --git a/build.gradle b/build.gradle index 04eb054c..26d4bbf8 100644 --- a/build.gradle +++ b/build.gradle @@ -179,9 +179,6 @@ dependencies { implementation 'com.googlecode.lambdaj:lambdaj:2.3.3' implementation 'com.google.googlejavaformat:google-java-format:1.18.1' -// implementation 'io.openapiprocessor:' -// implementation 'io.openapiprocessor:openapi-processor-spring:2023.4' - api 'commons-io:commons-io:2.12.0' api gradleApi() testImplementation 'org.mockito:mockito-core:4.5.1' diff --git a/gradle.properties b/gradle.properties index 224cbab4..82294404 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ package=co.com.bancolombia -systemProp.version=3.6.3-LOCAL +systemProp.version=3.6.3 simulateRest=true \ No newline at end of file diff --git a/src/main/java/co/com/bancolombia/utils/swagger/Swagger.java b/src/main/java/co/com/bancolombia/utils/swagger/Swagger.java index f6d1d161..7799882f 100644 --- a/src/main/java/co/com/bancolombia/utils/swagger/Swagger.java +++ b/src/main/java/co/com/bancolombia/utils/swagger/Swagger.java @@ -7,19 +7,12 @@ import java.io.File; import java.util.List; import java.util.Map; +import lombok.experimental.UtilityClass; import org.gradle.api.logging.Logger; +@UtilityClass public class Swagger { - public static void main(String[] args) { // TODO: Remove this method - Map params = Map.of("async", true, "lombok", true, "router", true); - generateEntrypoint( - "co.com.bancolombia", - "swagger3.yaml", - "build/clean/infrastructure/entry-points/reactive-web", - params); - } - public static void fromBuilder(ModuleBuilder builder, String outputDir) { String swaggerFile = builder.getStringParam("swagger-file"); if (swaggerFile != null) { @@ -31,7 +24,7 @@ public static void fromBuilder(ModuleBuilder builder, String outputDir) { "lombok", builder.isEnableLombok(), "router", - builder.getBooleanParam("task-param-router")); + builder.isReactive() && builder.getBooleanParam("task-param-router")); Logger logger = builder.getProject().getLogger(); generateEntrypoint( packageName, swaggerFile, builder.getProject().getRootDir() + "/" + outputDir, params) @@ -48,7 +41,7 @@ public static List generateEntrypoint( configurator.addAdditionalProperty("invokerPackage", packageName + ".api"); configurator.addAdditionalProperty("fullController", true); configurator.addAdditionalProperty("jakarta", true); - configurator.addAdditionalProperty("useBeanValidation", false); // TODO: make it to works + configurator.addAdditionalProperty("useBeanValidation", false); configurator.setInputSpecURL(swagger); configurator.setOutputDir(outputDir); configurator.setApiPackage(packageName + ".api"); diff --git a/src/main/java/io/swagger/codegen/v3/generators/WebFluxCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/WebFluxCodegen.java index 60b763a6..231480b6 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/WebFluxCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/WebFluxCodegen.java @@ -44,7 +44,7 @@ protected String getTemplateDir() { @Override public void processOpts() { super.processOpts(); - apiTestTemplateFiles.clear(); // TODO: implement it + apiTestTemplateFiles.clear(); apiDocTemplateFiles.clear(); apiTemplateFiles.clear(); importMapping.put("OffsetDateTime", "java.time.OffsetDateTime"); @@ -68,11 +68,7 @@ public void processOpts() { additionalProperties.put( "lambdaScapeVoid", (Mustache.Lambda) - (fragment, writer) -> { - System.out.println(fragment); - System.out.println(fragment.context()); - writer.write(fragment.execute().replaceAll("\\r|\\n", "")); - }); + (fragment, writer) -> writer.write(fragment.execute().replaceAll("\\r|\\n", ""))); } @Override diff --git a/src/test/java/co/com/bancolombia/task/GenerateEntryPointTaskTest.java b/src/test/java/co/com/bancolombia/task/GenerateEntryPointTaskTest.java index 4486ee03..76d179a0 100644 --- a/src/test/java/co/com/bancolombia/task/GenerateEntryPointTaskTest.java +++ b/src/test/java/co/com/bancolombia/task/GenerateEntryPointTaskTest.java @@ -22,6 +22,7 @@ import org.junit.Test; public class GenerateEntryPointTaskTest { + public static final String SWAGGER_FILE = "src/test/resources/swagger/pet-store.yaml"; private GenerateEntryPointTask task; @Before @@ -184,6 +185,25 @@ public void generateEntryPointApiRestWithDefaultServer() throws IOException, Cle .exists()); } + @Test + public void generateEntryPointApiRestWithDefaultServerFromSwaggerFile() + throws IOException, CleanException { + // Arrange + task.setType("RESTMVC"); + task.setFromSwagger(SWAGGER_FILE); + // Act + task.execute(); + // Assert + assertTrue( + new File( + "build/unitTest/infrastructure/entry-points/api-rest/src/main/java/co/com/bancolombia/api/PetApiController.java") + .exists()); + assertTrue( + new File( + "build/unitTest/infrastructure/entry-points/api-rest/src/main/java/co/com/bancolombia/api/model/Pet.java") + .exists()); + } + @Test public void generateEntryPointApiRestWithDefaultServerAndSwagger() throws IOException, CleanException { @@ -330,6 +350,27 @@ public void generateEntryPointReactiveWebWithoutRouterFunctions() .exists()); } + @Test + public void generateEntryPointReactiveWebWithoutRouterFunctionsFromSwagger() + throws IOException, CleanException { + // Arrange + setup(GenerateStructureTask.ProjectType.REACTIVE); + task.setType("WEBFLUX"); + task.setFromSwagger(SWAGGER_FILE); + task.setRouter(Constants.BooleanOption.FALSE); + // Act + task.execute(); + // Assert + assertTrue( + new File( + "build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/PetApiController.java") + .exists()); + assertTrue( + new File( + "build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/model/Pet.java") + .exists()); + } + @Test public void generateEntryPointReactiveWebWithoutRouterFunctionsAndSwagger() throws IOException, CleanException { @@ -392,6 +433,32 @@ public void generateEntryPointReactiveWebWithRouterFunctions() .exists()); } + @Test + public void generateEntryPointReactiveWebWithRouterFunctionsFromSwagger() + throws IOException, CleanException { + // Arrange + setup(GenerateStructureTask.ProjectType.REACTIVE); + task.setType("WEBFLUX"); + task.setFromSwagger(SWAGGER_FILE); + task.setRouter(Constants.BooleanOption.TRUE); + + // Act + task.execute(); + // Assert + assertTrue( + new File( + "build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/PetApiRouter.java") + .exists()); + assertTrue( + new File( + "build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/PetApiHandler.java") + .exists()); + assertTrue( + new File( + "build/unitTest/infrastructure/entry-points/reactive-web/src/main/java/co/com/bancolombia/api/model/Pet.java") + .exists()); + } + @Test public void generateEntryPointReactiveWebWithDefaultOptionFunctions() throws IOException, CleanException { diff --git a/src/test/resources/swagger/pet-store.yaml b/src/test/resources/swagger/pet-store.yaml new file mode 100644 index 00000000..e93def22 --- /dev/null +++ b/src/test/resources/swagger/pet-store.yaml @@ -0,0 +1,103 @@ +openapi: 3.0.1 +info: + title: Swagger Petstore 2.0 + description: 'This is a sample server Petstore server. You can find out more about Swagger + at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For + this sample, you can use the api key `special-key` to test the authorization filters.' + termsOfService: http://swagger.io/terms/ + contact: + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +externalDocs: + description: Find out more about Swagger + url: http://swagger.io +servers: + - url: https://petstore.swagger.io/v2 + - url: http://petstore.swagger.io/v2 +tags: + - name: pet + description: Everything about your Pets + externalDocs: + description: Find out more + url: http://swagger.io +paths: + /pet: + post: + tags: + - pet + summary: Add a new pet to the store + operationId: addPet + requestBody: + description: Pet object that needs to be added to the store + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + application/xml: + schema: + $ref: '#/components/schemas/Pet' + required: true + responses: + 201: + description: Pet created + 405: + description: Invalid input + content: {} + /pet/{petId}: + get: + tags: + - pet + summary: Find pet by ID + description: Returns a single pet + operationId: getPetById + parameters: + - name: petId + in: path + description: ID of pet to return + required: true + schema: + type: integer + format: int64 + responses: + 200: + description: successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + 400: + description: Invalid ID supplied + content: {} + 404: + description: Pet not found + content: {} +components: + schemas: + Pet: + required: + - name + - photoUrls + type: object + properties: + id: + type: integer + format: int64 +# example: 1 + name: + type: string +# example: doggie + photoUrls: + type: array + items: + type: string +# example: https://some.com/image.png + status: + type: string + description: pet status in the store + enum: + - AVAILABLE + - PENDING + - SOLD \ No newline at end of file