Skip to content

Commit

Permalink
Merge pull request #1115 from altro3/project_dir
Browse files Browse the repository at this point in the history
Add ability to set project directory.
  • Loading branch information
altro3 authored Jul 20, 2023
2 parents 73077de + e66b7bd commit 91babbf
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ managed-jakarta-validation-api = "3.0.2"
# Versions beyond 0.62.2 require Java 11
managed-html2md-converter = "0.62.2"

kotlin = "1.9.0"
kotlin = "1.8.22"
kotlin-coroutines = "1.7.2"
jspecify = "0.3.0"
jdt-annotation = "2.2.700"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
OpenApiApplicationVisitor.MICRONAUT_OPENAPI_SECURITY_ENABLED,
OpenApiApplicationVisitor.MICRONAUT_OPENAPI_VERSIONING_ENABLED,
OpenApiApplicationVisitor.MICRONAUT_OPENAPI_JSON_VIEW_DEFAULT_INCLUSION,
OpenApiApplicationVisitor.MICRONAUT_OPENAPI_PROJECT_DIR,
})
public class OpenApiApplicationVisitor extends AbstractOpenApiVisitor implements TypeElementVisitor<OpenAPIDefinition, Object> {

Expand Down Expand Up @@ -176,6 +177,14 @@ public class OpenApiApplicationVisitor extends AbstractOpenApiVisitor implements
* System property that specifies the location of additional swagger YAML and JSON files to read from.
*/
public static final String MICRONAUT_OPENAPI_ADDITIONAL_FILES = "micronaut.openapi.additional.files";
/**
* System property that specifies the location of current project.
*/
public static final String MICRONAUT_OPENAPI_PROJECT_DIR = "micronaut.openapi.project.dir";
/**
* Loaded project directory from system properties.
*/
public static final String MICRONAUT_INTERNAL_OPENAPI_PROJECT_DIR = "micronaut.internal.openapi.project.dir";
/**
* System property that specifies the default security schema name, if it's not specified by annotation SecurityScheme.
*/
Expand Down
31 changes: 24 additions & 7 deletions openapi/src/main/java/io/micronaut/openapi/visitor/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;

import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.MICRONAUT_INTERNAL_OPENAPI_PROJECT_DIR;
import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.MICRONAUT_OPENAPI_PROJECT_DIR;

/**
* Some util methods.
*
Expand Down Expand Up @@ -92,14 +95,28 @@ private Utils() {

@Nullable
public static Path getProjectPath(VisitorContext context) {
Path path;
try {
path = context.getProjectDir().orElse(Utils.isTestMode() ? Paths.get(System.getProperty("user.dir")) : null);
} catch (Exception e) {
// Should never happen
path = Paths.get(System.getProperty("user.dir"));

Path projectPath = context.get(MICRONAUT_INTERNAL_OPENAPI_PROJECT_DIR, Path.class).orElse(null);
if (projectPath != null) {
return projectPath;
}

String projectDir = System.getProperty(MICRONAUT_OPENAPI_PROJECT_DIR);
if (projectDir != null) {
projectPath = Paths.get(projectDir);
}
return path;
if (projectPath == null) {
try {
projectPath = context.getProjectDir().orElse(Utils.isTestMode() ? Paths.get(System.getProperty("user.dir")) : null);
} catch (Exception e) {
// Should never happen
projectPath = Paths.get(System.getProperty("user.dir"));
}
}

context.put(MICRONAUT_INTERNAL_OPENAPI_PROJECT_DIR, projectPath);

return projectPath;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/docs/guide/customSerializers.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
You can set custom classes to create different open api schemas for selected classes:

[source,yaml]
[configuration]
----
micronaut:
openapi:
Expand Down Expand Up @@ -77,7 +77,7 @@ class MyJaxbElement3 {

And set openapi properties to map classes to custom openapi schema classes:

[source,yaml]
[configuration]
----
micronaut:
openapi:
Expand Down
12 changes: 6 additions & 6 deletions src/main/docs/guide/exposingSwaggerOutput.adoc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
If you wish to expose the generated OpenAPI yaml output from your running application you can simply add the necessary static resource to the application configuration. For example:

.Exposing OpenAPI YAML
[source,yaml]
[configuration]
----
micronaut:
router:
static-resources:
swagger:
paths: classpath:META-INF/swagger
mapping: /swagger/**
router:
static-resources:
swagger:
paths: classpath:META-INF/swagger
mapping: /swagger/**
----

With the above configuration in place when you run your application you can access your Swagger documentation at `http://localhost:8080/swagger/hello-world-0.0.yml`.
12 changes: 12 additions & 0 deletions src/main/docs/guide/kotlin.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To support incremental annotation processing, you need to explicitly set the path to
the project directory through the annotation processor setting `micronaut.openapi.project.dir` like this:

.Gradle
[source,groovy]
```
kapt {
arguments {
arg("micronaut.openapi.project.dir", projectDir)
}
}
```
2 changes: 1 addition & 1 deletion src/main/docs/guide/openApiViews/mappingPath.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Thus, by default, the views expect to find the `yaml` under `/swagger`.
If you change this mapping to something else:

.Exposing Swagger YAML
[source,yaml]
[configuration]
----
micronaut:
router:
Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/openApiViews/rapidoc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ See https://mrin9.github.io/RapiDoc/api.html[RapiDoc Options] for a description.
To expose the `rapidoc` views, you also must expose the generated `yaml`:

.Exposing Swagger YAML And Rapidoc Views
[source,yaml]
[configuration]
----
micronaut:
router:
Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/openApiViews/redoc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ See https://github.com/Redocly/redoc#redoc-options-object[Redoc Options] for a d
To expose the `redoc` views, you also must expose the generated `yaml`:

.Exposing Swagger YAML And Redoc Views
[source,yaml]
[configuration]
----
micronaut:
router:
Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/openApiViews/swaggerui.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ See https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration
To expose the `swagger-ui` views, you also must expose the generated `yaml`:

.Exposing Swagger YAML and Swagger UI Views
[source,yaml]
[configuration]
----
micronaut:
router:
Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/openApiViews/swaggerui/oauth2.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class OrderController {

Do not forget to configure Micronaut Security accordingly:

[source,yaml]
[configuration]
----
micronaut:
security:
Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/schemaDecorators.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
If you have some classes with same names in different packages you can set postfix like this:

[source,yaml]
[configuration]
----
micronaut:
openapi:
Expand Down
1 change: 1 addition & 0 deletions src/main/docs/guide/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ controllers: OpenAPI Generation for Controllers
namingstrategy: Naming Strategy
customSerializers: Custom serializers
schemaDecorators: Schema decorators
kotlin: Kotlin specific
swaggerAnnotations:
title: Swagger Annotations
schemasAndPojos: Schemas and POJOs
Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/versionsAndGroups/groups.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ApiController {
}
----
[source,yaml]
[configuration]
----
micronaut:
openapi:
Expand Down
2 changes: 1 addition & 1 deletion src/main/docs/guide/versionsAndGroups/versions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class VersionedController {
----

.application.yml
[source,yaml]
[configuration]
----
micronaut:
router:
Expand Down

0 comments on commit 91babbf

Please sign in to comment.