Skip to content

Commit b005f81

Browse files
committed
Work on #51 - Replace shared API classes with codegen from OpenAPI/JSON schema - implemented FtgoApiDependencyResolverPlugin to 'download' and unpack API specification JARs
1 parent 78a4da2 commit b005f81

File tree

14 files changed

+75
-8
lines changed

14 files changed

+75
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import org.gradle.api.Plugin
2+
import org.gradle.api.Project
3+
4+
class FtgoApiDependencyResolverPlugin implements Plugin<Project> {
5+
6+
@Override
7+
void apply(Project project) {
8+
9+
project.ext.ftgoApiSpecsDir = "${project.buildDir}/ftgo-api-specs"
10+
11+
def c = project.configurations.create('ftgoApiSpecification')
12+
13+
project.configurations.implementation.extendsFrom(c)
14+
15+
project.task("ftgoResolveAPIDependencies",
16+
type: FtgoResolveAPIDependencies,
17+
group: 'build setup',
18+
description: "fetch API dependencies")
19+
}
20+
21+
}

buildSrc/src/main/groovy/FtgoJSONSchema2PojoPlugin.groovy

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class FtgoJSONSchema2PojoPlugin implements Plugin<Project> {
55

66
@Override
77
void apply(Project project) {
8+
9+
project.apply(plugin: FtgoApiDependencyResolverPlugin)
10+
811
def jsonSchemaSources = project.container(JSONSchemaSource)
912
project.extensions.add('ftgoJsonSchema2Pojo', jsonSchemaSources)
1013

@@ -19,6 +22,7 @@ class FtgoJSONSchema2PojoPlugin implements Plugin<Project> {
1922

2023
source.codeGen.targetDirectory = new File(project.buildDir, "generated-js2p-code-${source.name}")
2124

25+
source.codeGen.dependsOn(project.tasks.ftgoResolveAPIDependencies)
2226

2327
project.afterEvaluate {
2428
project.sourceSets[source.sourceSet].java.srcDirs += [source.codeGen.targetDirectory]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import org.gradle.api.tasks.Sync
2+
3+
class FtgoResolveAPIDependencies extends Sync {
4+
5+
FtgoResolveAPIDependencies() {
6+
7+
8+
from {
9+
project.configurations.ftgoApiSpecification.resolve().collect {
10+
project.zipTree(it)
11+
}
12+
}
13+
include("**/*.json")
14+
exclude("**/META-INF/**")
15+
into(project.ext.ftgoApiSpecsDir )
16+
17+
includeEmptyDirs = false
18+
19+
}
20+
21+
}

ftgo-common-jpa/src/main/resources/META-INF/orm.xml

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
</basic>
1111
</attributes>
1212
</embeddable>
13+
<embeddable class="net.chrisrichardson.ftgo.common.PersonName" access="FIELD">
14+
<attributes>
15+
<basic name="firstName">
16+
<column name="firstName"/>
17+
</basic>
18+
<basic name="lastName">
19+
<column name="lastName"/>
20+
</basic>
21+
</attributes>
22+
</embeddable>
1323
<embeddable class="net.chrisrichardson.ftgo.common.Address" access="FIELD">
1424
<attributes>
1525
<basic name="street1">

ftgo-common/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ dependencies {
44

55
compile "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"
66

7+
// compile "org.springframework.boot:spring-boot-starter:$springBootVersion"
8+
79
compile "io.eventuate.common:eventuate-common-json-mapper:$eventuateCommonVersion"
810

911
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"

ftgo-common/src/main/java/net/chrisrichardson/ftgo/common/PersonName.java

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package net.chrisrichardson.ftgo.common;
22

3-
import javax.persistence.Embeddable;
4-
5-
@Embeddable
63
public class PersonName {
74
private String firstName;
85
private String lastName;

ftgo-consumer-service/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616

1717
compile project(":common-swagger")
1818
compile project(":ftgo-consumer-service-api")
19+
compile project(":ftgo-consumer-service-api-spec")
1920

2021
compile "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"
2122
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"

ftgo-consumer-service/src/test/java/net/chrisrichardson/ftgo/consumerservice/api/ValidatingJSONMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.everit.json.schema.loader.SchemaLoader;
77
import org.json.JSONObject;
88
import org.json.JSONTokener;
9+
import org.springframework.util.Assert;
910

1011
import java.io.IOException;
1112
import java.io.InputStream;
@@ -23,6 +24,8 @@ public ValidatingJSONMapper(Schema schema) {
2324
public static ValidatingJSONMapper forSchema(String schemaPath) {
2425
Schema schema;
2526
try (InputStream inputStream = ValidatingJSONMapper.class.getResourceAsStream(schemaPath)) {
27+
if (inputStream == null)
28+
fail("Can't find schema: " + schemaPath);
2629
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
2730
schema = SchemaLoader.load(rawSchema);
2831
} catch (IOException e) {

ftgo-end-to-end-tests/build.gradle

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ plugins {
33
}
44

55
apply plugin: 'docker-compose'
6+
apply plugin: FtgoApiDependencyResolverPlugin
67

78
dependencies {
89
swaggerCodegen 'org.openapitools:openapi-generator-cli:3.3.4' // or OpenAPI Generator
910
}
1011

1112
swaggerSources {
1213
consumerService {
13-
inputFile = file('../ftgo-consumer-service-api/src/main/resources/ftgo-consumer-service-swagger.json')
14+
inputFile = file("${ftgoApiSpecsDir}/ftgo-consumer-service-swagger.json")
1415
code {
1516
language = 'java'
1617
configFile = file('swagger-codegen-config/consumer-service.json')
@@ -20,6 +21,8 @@ swaggerSources {
2021
}
2122
}
2223

24+
swaggerSources.consumerService.code.dependsOn ftgoResolveAPIDependencies
25+
2326
compileJava.dependsOn swaggerSources.consumerService.code
2427

2528
sourceSets.main.java.srcDir "${swaggerSources.consumerService.code.outputDir}/src/main/java"
@@ -29,6 +32,8 @@ sourceSets.main.java.srcDir "${swaggerSources.consumerService.code.outputDir}/sr
2932
sourceSets.main.resources.srcDir "${swaggerSources.consumerService.code.outputDir}/src/main/resources"
3033

3134
dependencies {
35+
ftgoApiSpecification project(":ftgo-consumer-service-api-spec")
36+
3237
compile project(":ftgo-accounting-service-api")
3338
compile project(":ftgo-consumer-service-api")
3439
compile project(":ftgo-kitchen-service-api")

ftgo-order-service/build.gradle

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ apply plugin: ComponentTestsPlugin
3535

3636
apply plugin: FtgoJSONSchema2PojoPlugin
3737

38-
3938
dependencyManagement {
4039
imports {
4140
mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:$springCloudContractDependenciesVersion"
@@ -114,9 +113,10 @@ idea {
114113
}
115114
}
116115

117-
118116
dependencies {
119117

118+
ftgoApiSpecification project(":ftgo-consumer-service-api-spec")
119+
ftgoApiSpecification project(":ftgo-accounting-service-api-spec")
120120

121121
compile project(":common-swagger")
122122
compile project(":ftgo-common-jpa")
@@ -133,6 +133,7 @@ dependencies {
133133
compile project(":ftgo-restaurant-service-api")
134134
compile project(":ftgo-order-service-api")
135135

136+
136137
compile "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"
137138
compile "org.springframework.boot:spring-boot-starter-actuator:$springBootVersion"
138139
compile "io.micrometer:micrometer-registry-prometheus:$micrometerVersion"
@@ -182,14 +183,14 @@ dependencies {
182183
ftgoJsonSchema2Pojo {
183184

184185
ftgoConsumerService {
185-
source = files("../ftgo-consumer-service-api/src/main/resources/ValidateOrderByConsumer.json")
186+
source = files("${ftgoApiSpecsDir}/ValidateOrderByConsumer.json")
186187
targetPackage = "net.chrisrichardson.ftgo.consumerservice.api"
187188
includeAdditionalProperties = false
188189
generateBuilders = true
189190
useLongIntegers = true
190191
}
191192
ftgoAccountingService {
192-
source = files("../ftgo-accounting-service-api/src/main/resources/messages")
193+
source = files("${ftgoApiSpecsDir}/messages")
193194
targetPackage = "net.chrisrichardson.ftgo.accountservice.api"
194195
includeAdditionalProperties = false
195196
generateBuilders = true

settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ include "ftgo-kitchen-service"
1515
include "ftgo-kitchen-service-contracts"
1616

1717
include "ftgo-accounting-service-api"
18+
include "ftgo-accounting-service-api-spec"
1819
include "ftgo-accounting-service"
1920
include "ftgo-accounting-service-contracts"
2021

2122
include "ftgo-consumer-service-api"
23+
include "ftgo-consumer-service-api-spec"
2224
include "ftgo-consumer-service"
2325
include "ftgo-consumer-service-contracts"
2426

0 commit comments

Comments
 (0)