Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set project directory. #1115

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you override getSupportedOptions() and provide all of these constants as annotation processor options

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, this block do the same

изображение

/**
* 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't use system properties but the annotation processor options otherwise the compiler can't detect when input values change

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand what's wrong here. As I understand it, it is necessary to describe the properties of the processor, which must be allowed to be loaded as system variables. They are described in the @SupportedOptions annotation. Now everything has been implemented.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general parameters to an annotation processor should never be ready from system properties and should always be read from annotation processor arguments, this particular processor abuses that badly in several places which is less than ideal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, then how is it correct to write the reading of the arguments? Although I'm not sure if there's much to worry about using system variables. I think it's redundant

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graemerocher Do I need to write something like this?

context.getOptions().get(MICRONAUT_INTERNAL_OPENAPI_PROJECT_DIR)

Copy link
Collaborator Author

@altro3 altro3 Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just that I don't understand. I give kapt the annotation processor argument, but I read it with a system property. That is, it turns out that all arguments are already available as system properties. Or does kapt set not an argument, but a system property?

I tested this solution on a test project - it works correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it needs to be context.getOptions().get(MICRONAUT_INTERNAL_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