Skip to content

Commit

Permalink
Merge pull request #359 from avaje/feature/358
Browse files Browse the repository at this point in the history
#358 Generated openapi.json should contain a servers section with loc…
  • Loading branch information
rbygrave authored Dec 13, 2023
2 parents 1532418 + 7d419a2 commit 1d537be
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
Expand All @@ -32,6 +31,7 @@
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.tags.Tag;

/** Context for building the OpenAPI documentation. */
Expand Down Expand Up @@ -65,10 +65,14 @@ public boolean isOpenApiAvailable() {
}

private OpenAPI initOpenAPI() {

OpenAPI openAPI = new OpenAPI();
openAPI.setPaths(new Paths());

Server server = new Server();
server.setUrl("localhost:8080");
server.setDescription("local testing");
openAPI.addServersItem(server);

Info info = new Info();
info.setTitle("");
info.setVersion("");
Expand Down Expand Up @@ -110,7 +114,6 @@ void addRequestBody(Operation operation, Schema schema, String mediaType, String
* Return the OpenAPI adding the paths and schemas.
*/
private OpenAPI getApiForWriting() {

Paths paths = openAPI.getPaths();
if (paths == null) {
paths = new Paths();
Expand Down Expand Up @@ -148,30 +151,29 @@ private Tag createTagItem(TagPrism tag) {

public void addTagsDefinition(Element element) {
final var tags = TagsPrism.getInstanceOn(element);
if (tags == null) return;

if (tags == null) {
return;
}
for(var tag : tags.value()){
openAPI.addTagsItem(createTagItem(tag));
}
}

public void addTagDefinition(Element element) {

for (var tag : TagPrism.getAllInstancesOn(element)) {
openAPI.addTagsItem(createTagItem(tag));
}
}

public void addSecurityScheme(Element element) {

this.addSecuritySchemes(SecuritySchemePrism.getAllInstancesOn(element));
}

public void addSecuritySchemes(Element element) {
var schemes = SecuritySchemesPrism.getInstanceOn(element);
if (schemes == null) {
return;
}
return;
}
this.addSecuritySchemes(schemes.value());
}

Expand All @@ -196,7 +198,6 @@ void addSecuritySchemes(List<SecuritySchemePrism> schemes) {
}

public void readApiDefinition(Element element) {

final var openApi = OpenAPIDefinitionPrism.getInstanceOn(element);
final var info = openApi.info();
if (!info.title().isEmpty()) {
Expand All @@ -217,7 +218,6 @@ public void writeApi() {

} catch (final Exception e) {
logError(null, "Error writing openapi file" + e.getMessage());
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"description" : "Example Javalin controllers with Java and Maven",
"version" : ""
},
"servers" : [
{
"url" : "localhost:8080",
"description" : "local testing"
}
],
"tags" : [
{
"name" : "tag1",
Expand Down Expand Up @@ -844,7 +850,7 @@
],
"responses" : {
"200" : {
"description" : "The Hello DTO given the id and name.",
"description" : "Return the Hello DTO.",
"content" : {
"application/json" : {
"schema" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"title" : "Example service showing off the Path extension method of controller",
"version" : ""
},
"servers" : [
{
"url" : "localhost:8080",
"description" : "local testing"
}
],
"tags" : [
{
"name" : "tag1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"description" : "Example Javalin controllers with Java and Maven",
"version" : ""
},
"servers" : [
{
"url" : "localhost:8080",
"description" : "local testing"
}
],
"tags" : [
{
"name" : "tag1",
Expand Down
8 changes: 7 additions & 1 deletion tests/test-javalin/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"description" : "Example Javalin controllers with Java and Maven",
"version" : ""
},
"servers" : [
{
"url" : "localhost:8080",
"description" : "local testing"
}
],
"paths" : {
"/bars" : {
"get" : {
Expand Down Expand Up @@ -781,7 +787,7 @@
],
"responses" : {
"200" : {
"description" : "The Hello DTO given the id and name.",
"description" : "Return the Hello DTO.",
"content" : {
"application/json" : {
"schema" : {
Expand Down
6 changes: 6 additions & 0 deletions tests/test-jex/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"title" : "",
"version" : ""
},
"servers" : [
{
"url" : "localhost:8080",
"description" : "local testing"
}
],
"paths" : {
"" : {
"get" : {
Expand Down

0 comments on commit 1d537be

Please sign in to comment.