Skip to content

Commit d974596

Browse files
ccozzolinowing328
authored andcommitted
[Java:vertx] Initialize router in init method and re-use router member to create S… (#7234)
* Initialize router in init method and re-use router member to create SwaggerRouter * Added generated samples for 7320 fix * Fixed alignment to 4 spaces for 7320 fix
1 parent 14e7158 commit d974596

File tree

5 files changed

+47
-45
lines changed

5 files changed

+47
-45
lines changed

modules/swagger-codegen/src/main/resources/JavaVertXServer/MainApiVerticle.mustache

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@ import com.github.phiz71.vertx.swagger.router.SwaggerRouter;
99
import io.swagger.models.Swagger;
1010
import io.swagger.parser.SwaggerParser;
1111
import io.vertx.core.AbstractVerticle;
12+
import io.vertx.core.Context;
1213
import io.vertx.core.Future;
1314
import io.vertx.core.file.FileSystem;
1415
import io.vertx.core.json.Json;
1516
import io.vertx.core.logging.Logger;
1617
import io.vertx.core.logging.LoggerFactory;
18+
import io.vertx.core.Vertx;
1719
import io.vertx.ext.web.Router;
1820

1921
public class MainApiVerticle extends AbstractVerticle {
2022
final static Logger LOGGER = LoggerFactory.getLogger(MainApiVerticle.class);
2123
22-
final Router router = Router.router(vertx);
23-
24+
protected Router router;
25+
26+
@Override
27+
public void init(Vertx vertx, Context context) {
28+
super.init(vertx, context);
29+
router = Router.router(vertx);
30+
}
31+
2432
@Override
2533
public void start(Future<Void> startFuture) throws Exception {
2634
Json.mapper.registerModule(new JavaTimeModule());
27-
FileSystem vertxFileSystem = vertx.fileSystem();
35+
FileSystem vertxFileSystem = vertx.fileSystem();
2836
vertxFileSystem.readFile("swagger.json", readFile -> {
2937
if (readFile.succeeded()) {
3038
Swagger swagger = new SwaggerParser().parse(readFile.result().toString(Charset.forName("utf-8")));
31-
Router swaggerRouter = SwaggerRouter.swaggerRouter(Router.router(vertx), swagger, vertx.eventBus(), new OperationIdServiceIdResolver());
39+
Router swaggerRouter = SwaggerRouter.swaggerRouter(router, swagger, vertx.eventBus(), new OperationIdServiceIdResolver());
3240
3341
deployVerticles(startFuture);
3442
@@ -49,7 +57,7 @@ public class MainApiVerticle extends AbstractVerticle {
4957
LOGGER.info("{{classname}}Verticle : Deployed");
5058
} else {
5159
startFuture.fail(res.cause());
52-
LOGGER.error("{{classname}}Verticle : Deployement failed");
60+
LOGGER.error("{{classname}}Verticle : Deployment failed");
5361
}
5462
});
5563
{{/apis}}{{/apiInfo}}

samples/server/petstore/java-vertx/async/src/main/java/io/swagger/server/api/MainApiVerticle.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@
99
import io.swagger.models.Swagger;
1010
import io.swagger.parser.SwaggerParser;
1111
import io.vertx.core.AbstractVerticle;
12+
import io.vertx.core.Context;
1213
import io.vertx.core.Future;
1314
import io.vertx.core.file.FileSystem;
1415
import io.vertx.core.json.Json;
1516
import io.vertx.core.logging.Logger;
1617
import io.vertx.core.logging.LoggerFactory;
18+
import io.vertx.core.Vertx;
1719
import io.vertx.ext.web.Router;
1820

1921
public class MainApiVerticle extends AbstractVerticle {
2022
final static Logger LOGGER = LoggerFactory.getLogger(MainApiVerticle.class);
2123

22-
final Router router = Router.router(vertx);
23-
24+
protected Router router;
25+
26+
@Override
27+
public void init(Vertx vertx, Context context) {
28+
super.init(vertx, context);
29+
router = Router.router(vertx);
30+
}
31+
2432
@Override
2533
public void start(Future<Void> startFuture) throws Exception {
2634
Json.mapper.registerModule(new JavaTimeModule());
27-
FileSystem vertxFileSystem = vertx.fileSystem();
35+
FileSystem vertxFileSystem = vertx.fileSystem();
2836
vertxFileSystem.readFile("swagger.json", readFile -> {
2937
if (readFile.succeeded()) {
3038
Swagger swagger = new SwaggerParser().parse(readFile.result().toString(Charset.forName("utf-8")));
31-
Router swaggerRouter = SwaggerRouter.swaggerRouter(Router.router(vertx), swagger, vertx.eventBus(), new OperationIdServiceIdResolver());
39+
Router swaggerRouter = SwaggerRouter.swaggerRouter(router, swagger, vertx.eventBus(), new OperationIdServiceIdResolver());
3240

3341
deployVerticles(startFuture);
3442

@@ -49,7 +57,7 @@ public void deployVerticles(Future<Void> startFuture) {
4957
LOGGER.info("PetApiVerticle : Deployed");
5058
} else {
5159
startFuture.fail(res.cause());
52-
LOGGER.error("PetApiVerticle : Deployement failed");
60+
LOGGER.error("PetApiVerticle : Deployment failed");
5361
}
5462
});
5563

@@ -58,7 +66,7 @@ public void deployVerticles(Future<Void> startFuture) {
5866
LOGGER.info("StoreApiVerticle : Deployed");
5967
} else {
6068
startFuture.fail(res.cause());
61-
LOGGER.error("StoreApiVerticle : Deployement failed");
69+
LOGGER.error("StoreApiVerticle : Deployment failed");
6270
}
6371
});
6472

@@ -67,7 +75,7 @@ public void deployVerticles(Future<Void> startFuture) {
6775
LOGGER.info("UserApiVerticle : Deployed");
6876
} else {
6977
startFuture.fail(res.cause());
70-
LOGGER.error("UserApiVerticle : Deployement failed");
78+
LOGGER.error("UserApiVerticle : Deployment failed");
7179
}
7280
});
7381

samples/server/petstore/java-vertx/async/src/main/resources/swagger.json

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@
112112
"type" : "array",
113113
"items" : {
114114
"type" : "string",
115-
"enum" : [ "available", "pending", "sold" ],
116-
"default" : "available"
115+
"default" : "available",
116+
"enum" : [ "available", "pending", "sold" ]
117117
},
118118
"collectionFormat" : "csv"
119119
} ],
@@ -134,7 +134,6 @@
134134
"security" : [ {
135135
"petstore_auth" : [ "write:pets", "read:pets" ]
136136
} ],
137-
"x-contentType" : "application/json",
138137
"x-accepts" : "application/json"
139138
}
140139
},
@@ -174,7 +173,6 @@
174173
"petstore_auth" : [ "write:pets", "read:pets" ]
175174
} ],
176175
"deprecated" : true,
177-
"x-contentType" : "application/json",
178176
"x-accepts" : "application/json"
179177
}
180178
},
@@ -210,7 +208,6 @@
210208
"security" : [ {
211209
"api_key" : [ ]
212210
} ],
213-
"x-contentType" : "application/json",
214211
"x-accepts" : "application/json"
215212
},
216213
"post" : {
@@ -278,7 +275,6 @@
278275
"security" : [ {
279276
"petstore_auth" : [ "write:pets", "read:pets" ]
280277
} ],
281-
"x-contentType" : "application/json",
282278
"x-accepts" : "application/json"
283279
}
284280
},
@@ -348,7 +344,6 @@
348344
"security" : [ {
349345
"api_key" : [ ]
350346
} ],
351-
"x-contentType" : "application/json",
352347
"x-accepts" : "application/json"
353348
}
354349
},
@@ -414,7 +409,6 @@
414409
"description" : "Order not found"
415410
}
416411
},
417-
"x-contentType" : "application/json",
418412
"x-accepts" : "application/json"
419413
},
420414
"delete" : {
@@ -438,7 +432,6 @@
438432
"description" : "Order not found"
439433
}
440434
},
441-
"x-contentType" : "application/json",
442435
"x-accepts" : "application/json"
443436
}
444437
},
@@ -566,7 +559,6 @@
566559
"description" : "Invalid username/password supplied"
567560
}
568561
},
569-
"x-contentType" : "application/json",
570562
"x-accepts" : "application/json"
571563
}
572564
},
@@ -583,7 +575,6 @@
583575
"description" : "successful operation"
584576
}
585577
},
586-
"x-contentType" : "application/json",
587578
"x-accepts" : "application/json"
588579
}
589580
},
@@ -615,7 +606,6 @@
615606
"description" : "User not found"
616607
}
617608
},
618-
"x-contentType" : "application/json",
619609
"x-accepts" : "application/json"
620610
},
621611
"put" : {
@@ -671,7 +661,6 @@
671661
"description" : "User not found"
672662
}
673663
},
674-
"x-contentType" : "application/json",
675664
"x-accepts" : "application/json"
676665
}
677666
}

samples/server/petstore/java-vertx/rx/src/main/java/io/swagger/server/api/MainApiVerticle.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@
99
import io.swagger.models.Swagger;
1010
import io.swagger.parser.SwaggerParser;
1111
import io.vertx.core.AbstractVerticle;
12+
import io.vertx.core.Context;
1213
import io.vertx.core.Future;
1314
import io.vertx.core.file.FileSystem;
1415
import io.vertx.core.json.Json;
1516
import io.vertx.core.logging.Logger;
1617
import io.vertx.core.logging.LoggerFactory;
18+
import io.vertx.core.Vertx;
1719
import io.vertx.ext.web.Router;
1820

1921
public class MainApiVerticle extends AbstractVerticle {
2022
final static Logger LOGGER = LoggerFactory.getLogger(MainApiVerticle.class);
2123

22-
final Router router = Router.router(vertx);
23-
24+
protected Router router;
25+
26+
@Override
27+
public void init(Vertx vertx, Context context) {
28+
super.init(vertx, context);
29+
router = Router.router(vertx);
30+
}
31+
2432
@Override
2533
public void start(Future<Void> startFuture) throws Exception {
2634
Json.mapper.registerModule(new JavaTimeModule());
27-
FileSystem vertxFileSystem = vertx.fileSystem();
35+
FileSystem vertxFileSystem = vertx.fileSystem();
2836
vertxFileSystem.readFile("swagger.json", readFile -> {
2937
if (readFile.succeeded()) {
3038
Swagger swagger = new SwaggerParser().parse(readFile.result().toString(Charset.forName("utf-8")));
31-
Router swaggerRouter = SwaggerRouter.swaggerRouter(Router.router(vertx), swagger, vertx.eventBus(), new OperationIdServiceIdResolver());
39+
Router swaggerRouter = SwaggerRouter.swaggerRouter(router, swagger, vertx.eventBus(), new OperationIdServiceIdResolver());
3240

3341
deployVerticles(startFuture);
3442

@@ -49,7 +57,7 @@ public void deployVerticles(Future<Void> startFuture) {
4957
LOGGER.info("PetApiVerticle : Deployed");
5058
} else {
5159
startFuture.fail(res.cause());
52-
LOGGER.error("PetApiVerticle : Deployement failed");
60+
LOGGER.error("PetApiVerticle : Deployment failed");
5361
}
5462
});
5563

@@ -58,7 +66,7 @@ public void deployVerticles(Future<Void> startFuture) {
5866
LOGGER.info("StoreApiVerticle : Deployed");
5967
} else {
6068
startFuture.fail(res.cause());
61-
LOGGER.error("StoreApiVerticle : Deployement failed");
69+
LOGGER.error("StoreApiVerticle : Deployment failed");
6270
}
6371
});
6472

@@ -67,7 +75,7 @@ public void deployVerticles(Future<Void> startFuture) {
6775
LOGGER.info("UserApiVerticle : Deployed");
6876
} else {
6977
startFuture.fail(res.cause());
70-
LOGGER.error("UserApiVerticle : Deployement failed");
78+
LOGGER.error("UserApiVerticle : Deployment failed");
7179
}
7280
});
7381

samples/server/petstore/java-vertx/rx/src/main/resources/swagger.json

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@
112112
"type" : "array",
113113
"items" : {
114114
"type" : "string",
115-
"enum" : [ "available", "pending", "sold" ],
116-
"default" : "available"
115+
"default" : "available",
116+
"enum" : [ "available", "pending", "sold" ]
117117
},
118118
"collectionFormat" : "csv"
119119
} ],
@@ -134,7 +134,6 @@
134134
"security" : [ {
135135
"petstore_auth" : [ "write:pets", "read:pets" ]
136136
} ],
137-
"x-contentType" : "application/json",
138137
"x-accepts" : "application/json"
139138
}
140139
},
@@ -174,7 +173,6 @@
174173
"petstore_auth" : [ "write:pets", "read:pets" ]
175174
} ],
176175
"deprecated" : true,
177-
"x-contentType" : "application/json",
178176
"x-accepts" : "application/json"
179177
}
180178
},
@@ -210,7 +208,6 @@
210208
"security" : [ {
211209
"api_key" : [ ]
212210
} ],
213-
"x-contentType" : "application/json",
214211
"x-accepts" : "application/json"
215212
},
216213
"post" : {
@@ -278,7 +275,6 @@
278275
"security" : [ {
279276
"petstore_auth" : [ "write:pets", "read:pets" ]
280277
} ],
281-
"x-contentType" : "application/json",
282278
"x-accepts" : "application/json"
283279
}
284280
},
@@ -348,7 +344,6 @@
348344
"security" : [ {
349345
"api_key" : [ ]
350346
} ],
351-
"x-contentType" : "application/json",
352347
"x-accepts" : "application/json"
353348
}
354349
},
@@ -414,7 +409,6 @@
414409
"description" : "Order not found"
415410
}
416411
},
417-
"x-contentType" : "application/json",
418412
"x-accepts" : "application/json"
419413
},
420414
"delete" : {
@@ -438,7 +432,6 @@
438432
"description" : "Order not found"
439433
}
440434
},
441-
"x-contentType" : "application/json",
442435
"x-accepts" : "application/json"
443436
}
444437
},
@@ -566,7 +559,6 @@
566559
"description" : "Invalid username/password supplied"
567560
}
568561
},
569-
"x-contentType" : "application/json",
570562
"x-accepts" : "application/json"
571563
}
572564
},
@@ -583,7 +575,6 @@
583575
"description" : "successful operation"
584576
}
585577
},
586-
"x-contentType" : "application/json",
587578
"x-accepts" : "application/json"
588579
}
589580
},
@@ -615,7 +606,6 @@
615606
"description" : "User not found"
616607
}
617608
},
618-
"x-contentType" : "application/json",
619609
"x-accepts" : "application/json"
620610
},
621611
"put" : {
@@ -671,7 +661,6 @@
671661
"description" : "User not found"
672662
}
673663
},
674-
"x-contentType" : "application/json",
675664
"x-accepts" : "application/json"
676665
}
677666
}

0 commit comments

Comments
 (0)