forked from jhipster/jhipster-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new module for generating OpenAPI contract at build-time for Sp…
…ringMVC applications
- Loading branch information
Showing
10 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...gboot/apidocumentation/openapicontract/application/OpenApiContractApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package tech.jhipster.lite.generator.server.springboot.apidocumentation.openapicontract.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import tech.jhipster.lite.generator.server.springboot.apidocumentation.openapicontract.domain.OpenApiContractModuleFactory; | ||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
|
||
@Service | ||
public class OpenApiContractApplicationService { | ||
|
||
private final OpenApiContractModuleFactory factory; | ||
|
||
public OpenApiContractApplicationService() { | ||
factory = new OpenApiContractModuleFactory(); | ||
} | ||
|
||
public JHipsterModule buildModule(JHipsterModuleProperties properties) { | ||
return factory.buildModule(properties); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...rver/springboot/apidocumentation/openapicontract/domain/OpenApiContractModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package tech.jhipster.lite.generator.server.springboot.apidocumentation.openapicontract.domain; | ||
|
||
import static tech.jhipster.lite.module.domain.JHipsterModule.*; | ||
|
||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin; | ||
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin.MavenPluginOptionalBuilder; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
import tech.jhipster.lite.shared.error.domain.Assert; | ||
|
||
public class OpenApiContractModuleFactory { | ||
|
||
public JHipsterModule buildModule(JHipsterModuleProperties properties) { | ||
Assert.notNull("properties", properties); | ||
|
||
//@formatter:off | ||
return moduleBuilder(properties) | ||
.mavenPlugins() | ||
.pluginManagement(openApiPluginManagement(properties)) | ||
.plugin(openApiPlugin().build()) | ||
.and() | ||
.build(); | ||
//@formatter:on | ||
} | ||
|
||
private MavenPluginOptionalBuilder openApiPlugin() { | ||
return mavenPlugin().groupId("io.github.kbuntrock").artifactId("openapi-maven-plugin"); | ||
} | ||
|
||
private MavenPlugin openApiPluginManagement(JHipsterModuleProperties properties) { | ||
return openApiPlugin() | ||
.versionSlug("openapi-maven-plugin") | ||
.addExecution(pluginExecution().goals("documentation").id("generate-openapi-contract")) | ||
.configuration( | ||
""" | ||
<apiConfiguration> | ||
<library>SPRING_MVC</library> | ||
</apiConfiguration> | ||
<apis> | ||
<api> | ||
<filename>openapi-contract.yml</filename> | ||
<locations> | ||
<location>%s</location> | ||
</locations> | ||
<tag> | ||
<substitutions> | ||
<sub> | ||
<regex>Resource$</regex> | ||
<substitute /> | ||
</sub> | ||
</substitutions> | ||
</tag> | ||
</api> | ||
</apis>\ | ||
""".formatted(properties.basePackage()) | ||
) | ||
.build(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...umentation/openapicontract/infrastructure/primary/OpenApiContractModuleConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tech.jhipster.lite.generator.server.springboot.apidocumentation.openapicontract.infrastructure.primary; | ||
|
||
import static tech.jhipster.lite.generator.slug.domain.JHLiteFeatureSlug.SPRING_MVC_SERVER; | ||
import static tech.jhipster.lite.generator.slug.domain.JHLiteModuleSlug.*; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import tech.jhipster.lite.generator.server.springboot.apidocumentation.openapicontract.application.OpenApiContractApplicationService; | ||
import tech.jhipster.lite.module.domain.resource.*; | ||
|
||
@Configuration | ||
class OpenApiContractModuleConfiguration { | ||
|
||
@Bean | ||
JHipsterModuleResource openApiContractModule(OpenApiContractApplicationService openApiContract) { | ||
return JHipsterModuleResource.builder() | ||
.slug(OPENAPI_CONTRACT) | ||
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().build()) | ||
.apiDoc("Spring Boot - OpenAPI", "Generates OpenAPI contract at build time using openapi-maven-plugin") | ||
.organization(JHipsterModuleOrganization.builder().addDependency(SPRING_MVC_SERVER).addDependency(MAVEN_JAVA).build()) | ||
.tags("server", "spring", "spring-boot", "documentation", "swagger", "openapi") | ||
.factory(openApiContract::buildModule); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...pster/lite/generator/server/springboot/apidocumentation/openapicontract/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@tech.jhipster.lite.BusinessContext | ||
package tech.jhipster.lite.generator.server.springboot.apidocumentation.openapicontract; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Feature: OpenAPI contract generation | ||
|
||
Scenario: Should apply OpenAPI contract module | ||
When I apply "openapi-contract" module to default project with maven file | ||
| packageName | tech.jhipster.chips | | ||
Then I should have "openapi-maven-plugin" in "pom.xml" |
75 changes: 75 additions & 0 deletions
75
.../springboot/apidocumentation/openapicontract/domain/OpenApiContractModuleFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package tech.jhipster.lite.generator.server.springboot.apidocumentation.openapicontract.domain; | ||
|
||
import static tech.jhipster.lite.TestFileUtils.tmpDirForTest; | ||
import static tech.jhipster.lite.module.domain.JHipsterModulesFixture.propertiesBuilder; | ||
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles; | ||
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.pomFile; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import tech.jhipster.lite.UnitTest; | ||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
|
||
@UnitTest | ||
class OpenApiContractModuleFactoryTest { | ||
|
||
private static final OpenApiContractModuleFactory factory = new OpenApiContractModuleFactory(); | ||
|
||
@Test | ||
void shouldBuildModuleOnProjectWithoutDefaultGoal() { | ||
JHipsterModuleProperties properties = propertiesBuilder(tmpDirForTest()).basePackage("tech.jhipster.jhlitest").build(); | ||
|
||
JHipsterModule module = factory.buildModule(properties); | ||
|
||
assertThatModuleWithFiles(module, pomFile()) | ||
.hasFile("pom.xml") | ||
.containing( | ||
// language=xml | ||
""" | ||
<plugin> | ||
<groupId>io.github.kbuntrock</groupId> | ||
<artifactId>openapi-maven-plugin</artifactId> | ||
</plugin>\ | ||
""" | ||
) | ||
.containing( | ||
// language=xml | ||
""" | ||
<plugin> | ||
<groupId>io.github.kbuntrock</groupId> | ||
<artifactId>openapi-maven-plugin</artifactId> | ||
<version>${openapi-maven-plugin.version}</version> | ||
<executions> | ||
<execution> | ||
<id>generate-openapi-contract</id> | ||
<goals> | ||
<goal>documentation</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<apiConfiguration> | ||
<library>SPRING_MVC</library> | ||
</apiConfiguration> | ||
<apis> | ||
<api> | ||
<filename>openapi-contract.yml</filename> | ||
<locations> | ||
<location>tech.jhipster.jhlitest</location> | ||
</locations> | ||
<tag> | ||
<substitutions> | ||
<sub> | ||
<regex>Resource$</regex> | ||
<substitute /> | ||
</sub> | ||
</substitutions> | ||
</tag> | ||
</api> | ||
</apis> | ||
</configuration> | ||
</plugin>\ | ||
""" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters