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

Micronaut Spring 5.8.1 #1821

Merged
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 @@ -40,7 +40,7 @@ micronaut-kotlin = "4.4.0"
micronaut-logging = "1.5.0"
micronaut-session = "4.4.0"
micronaut-grpc = "4.7.1"
micronaut-spring = "5.8.0"
micronaut-spring = "5.8.1"
micronaut-docs = "2.0.0"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,9 @@ private Parameter processMethodParameterAnnotation(VisitorContext context, Opera
}
} else if (parameter.isAnnotationPresent(PathVariable.class)) {
String paramName = parameter.getValue(PathVariable.class, String.class).orElse(parameterName);
if (paramName.isEmpty()) {
paramName = parameterName;
}
UriMatchVariable variable = pathVariables.get(paramName);
if (variable == null) {
warn("Path variable name: '" + paramName + "' not found in path, operation: " + swaggerOperation.getOperationId(), context, parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,14 @@ public static List<Pair<String, String>> getExpandableProperties(VisitorContext
}

var expandableProperties = new ArrayList<Pair<String, String>>();
var expandPrefix = MICRONAUT_OPENAPI_EXPAND_PREFIX + DOT;

// first, check system properties and environments config files
var env = (AnnProcessorEnvironment) getEnv(context);
Map<String, Object> propertiesFromEnv = null;
if (env != null) {
try {
propertiesFromEnv = env.getProperties(MICRONAUT_OPENAPI_EXPAND_PREFIX.substring(0, MICRONAUT_OPENAPI_EXPAND_PREFIX.length() - 1), null);
propertiesFromEnv = env.getProperties(expandPrefix.substring(0, expandPrefix.length() - 1), null);
} catch (Exception e) {
warn("Error:\n" + Utils.printStackTrace(e), context);
}
Expand All @@ -347,7 +348,7 @@ public static List<Pair<String, String>> getExpandableProperties(VisitorContext
Properties openapiProps = readOpenApiConfigFile(context);
for (Map.Entry<Object, Object> entry : openapiProps.entrySet()) {
String key = entry.getKey().toString();
if (!key.startsWith(MICRONAUT_OPENAPI_EXPAND_PREFIX)) {
if (!key.startsWith(expandPrefix)) {
continue;
}
expandedPropsMap.put(key, entry.getValue().toString());
Expand All @@ -357,7 +358,7 @@ public static List<Pair<String, String>> getExpandableProperties(VisitorContext
if (CollectionUtils.isNotEmpty(System.getProperties())) {
for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
String key = entry.getKey().toString();
if (!key.startsWith(MICRONAUT_OPENAPI_EXPAND_PREFIX)) {
if (!key.startsWith(expandPrefix)) {
continue;
}
expandedPropsMap.put(key, entry.getValue().toString());
Expand All @@ -366,8 +367,8 @@ public static List<Pair<String, String>> getExpandableProperties(VisitorContext

for (Map.Entry<String, String> entry : expandedPropsMap.entrySet()) {
String key = entry.getKey();
if (key.startsWith(MICRONAUT_OPENAPI_EXPAND_PREFIX)) {
key = key.substring(MICRONAUT_OPENAPI_EXPAND_PREFIX.length());
if (key.startsWith(expandPrefix)) {
key = key.substring(expandPrefix.length());
}
var prop = Pair.of("\\$\\{" + key + '}', entry.getValue());
if (!expandableProperties.contains(prop)) {
Expand All @@ -390,6 +391,8 @@ public static Map<String, String> getAdocProperties(OpenApiInfo openApiInfo, boo
adocProperties.put(MICRONAUT_OPENAPI_ADOC_OUTPUT_FILENAME, getConfigProperty(MICRONAUT_OPENAPI_ADOC_OUTPUT_FILENAME, context));
adocProperties.put(MICRONAUT_OPENAPI_ADOC_OPENAPI_PATH, getConfigProperty(MICRONAUT_OPENAPI_ADOC_OPENAPI_PATH, context));

var expandPrefix = MICRONAUT_OPENAPI_EXPAND_PREFIX + DOT;

// first, check system properties and environments config files
var env = (AnnProcessorEnvironment) getEnv(context);
Map<String, Object> propertiesFromEnv = null;
Expand All @@ -411,7 +414,7 @@ public static Map<String, String> getAdocProperties(OpenApiInfo openApiInfo, boo
Properties openapiProps = readOpenApiConfigFile(context);
for (Map.Entry<Object, Object> entry : openapiProps.entrySet()) {
String key = entry.getKey().toString();
if (!key.startsWith(MICRONAUT_OPENAPI_EXPAND_PREFIX)) {
if (!key.startsWith(expandPrefix)) {
continue;
}
adocProperties.put(key, entry.getValue().toString());
Expand All @@ -421,7 +424,7 @@ public static Map<String, String> getAdocProperties(OpenApiInfo openApiInfo, boo
if (CollectionUtils.isNotEmpty(System.getProperties())) {
for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
String key = entry.getKey().toString();
if (!key.startsWith(MICRONAUT_OPENAPI_EXPAND_PREFIX)) {
if (!key.startsWith(expandPrefix)) {
continue;
}
adocProperties.put(key, entry.getValue().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_JSON_FORMAT;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_PROPERTY_NAMING_STRATEGY;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_VIEWS_SPEC;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.SPRING_APPLICATION_NAME;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_SECURITY;
import static io.micronaut.openapi.visitor.OpenApiNormalizeUtils.findAndRemoveDuplicates;
import static io.micronaut.openapi.visitor.OpenApiNormalizeUtils.normalizeOpenApi;
Expand Down Expand Up @@ -741,6 +742,9 @@ private void fixInfoBlockIfNeeded(OpenAPI openApi, VisitorContext context) {
var info = openApi.getInfo();
if (info.getTitle() == null) {
String applicationName = ConfigUtils.getConfigProperty(MICRONAUT_APPLICATION_NAME, context);
if (applicationName == null) {
applicationName = ConfigUtils.getConfigProperty(SPRING_APPLICATION_NAME, context);
}
info.setTitle(applicationName != null ? applicationName : DEFAULT_OPENAPI_TITLE);
}
if (info.getVersion() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public interface OpenApiConfigProperty {
/**
* Prefix for expandable properties.
*/
String MICRONAUT_OPENAPI_EXPAND_PREFIX = "micronaut.openapi.expand.";
String MICRONAUT_OPENAPI_EXPAND_PREFIX = "micronaut.openapi.expand";
/**
* System property for server context path.
*/
Expand Down Expand Up @@ -204,6 +204,10 @@ public interface OpenApiConfigProperty {
* micronaut-context application name property.
*/
String MICRONAUT_APPLICATION_NAME = "micronaut.application.name";
/**
* spring-context application name property.
*/
String SPRING_APPLICATION_NAME = "spring.application.name";
/**
* If this property is 'true', then generated OpenAPI specification will be with extensions for OpenAPI Generator
* and the generated client according to this specification will be much more accurate than without it.
Expand Down Expand Up @@ -381,6 +385,7 @@ public interface OpenApiConfigProperty {
MICRONAUT_OPENAPI_SWAGGER_FILE_GENERATION_ENABLED,
MICRONAUT_OPENAPI_SCHEMA_EXTRA_ENABLED,
MICRONAUT_OPENAPI_SCHEMA_NAME_SEPARATOR_EMPTY,
MICRONAUT_OPENAPI_SCHEMA_NAME_SEPARATOR_GENERIC
MICRONAUT_OPENAPI_SCHEMA_NAME_SEPARATOR_GENERIC,
MICRONAUT_OPENAPI_EXPAND_PREFIX
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,44 @@ class MyController {
}
}

@jakarta.inject.Singleton
class MyBean {}
''')
then:
Utils.testReference != null

when:
OpenAPI openAPI = Utils.testReference

then:
openAPI.info
openAPI.info.title == serviceName
openAPI.info.version == OpenApiApplicationVisitor.DEFAULT_OPENAPI_VERSION
}

@RestoreSystemProperties
void "test auto generated info block with spring application name"() {

given:
def serviceName = "This is my service"
System.setProperty(OpenApiConfigProperty.SPRING_APPLICATION_NAME, serviceName)

when:
buildBeanDefinition('test.MyBean', '''
package test;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller
class MyController {

@Get("/get")
public String get() {
return null;
}
}

@jakarta.inject.Singleton
class MyBean {}
''')
Expand Down
1 change: 1 addition & 0 deletions test-suite-java-spring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ tasks.withType(JavaCompile).configureEach {
'-Xlint:unchecked',
'-Xlint:deprecation'
]
options.forkOptions.jvmArgs += "-Dapp.version=myVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
@SpringBootApplication
@OpenAPIDefinition(
info = @Info(
title = "demo",
version = "0.0"
version = "${app.version}"
)
)
public class Application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

import static io.micronaut.openapi.spring.TestConfig.APP_NAME;
import static io.micronaut.openapi.spring.TestConfig.APP_VERSION;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -29,15 +31,15 @@ class OpenApiExposedTest {
void testOpenApiSpecEndpoint() throws IOException {

String openApiSpec;
try (var is = getClass().getResourceAsStream("/META-INF/swagger/demo-0.0.yml")) {
try (var is = getClass().getResourceAsStream("/META-INF/swagger/" + APP_NAME + '-' + APP_VERSION + ".yml")) {
assertNotNull(is);
openApiSpec = new String(is.readAllBytes());
}
var recievedOpenApiSpec = new AtomicReference<String>();

assertDoesNotThrow(() -> {
var result = restClient.get()
.uri("/swagger/demo-0.0.yml")
.uri("/swagger/" + APP_NAME + '-' + APP_VERSION + ".yml")
.retrieve();

recievedOpenApiSpec.set(result.body(String.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
@Configuration
public class TestConfig {

public static final String APP_NAME = "test-suite-java-spring";
public static final String APP_VERSION = "myVersion";

@Bean
RestClient restClient(@Value("${server.port:8080}") int port) {
return RestClient.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import io.micronaut.openapi.OpenApiUtils;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestClient;

import java.util.List;

import static io.micronaut.openapi.spring.TestConfig.APP_NAME;
import static io.micronaut.openapi.spring.TestConfig.APP_VERSION;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ActiveProfiles("test")
Expand All @@ -27,11 +33,13 @@ class TestControllerTest {
@Test
void springOpenApiPathTest() throws JsonProcessingException {
var result = restClient.get()
.uri("/swagger/demo-0.0.yml")
.uri("/swagger/" + APP_NAME + '-' + APP_VERSION + ".yml")
.retrieve()
.body(String.class);

var openApi = OpenApiUtils.getYamlMapper().readValue(result, OpenAPI.class);
assertNotNull(openApi.getInfo());
assertEquals(APP_VERSION, openApi.getInfo().getVersion());
assertNotNull(openApi.getPaths());

var userSchema = openApi.getComponents().getSchemas().get("User");
Expand Down Expand Up @@ -72,17 +80,30 @@ void springOpenApiPathTest() throws JsonProcessingException {
assertNotNull(params);
assertEquals(2, params.size());

assertEquals("userId", params.get(0).getName());
assertEquals("path", params.get(0).getIn());
assertTrue(params.get(0).getRequired());
assertNotNull(params.get(0).getSchema());
assertEquals("string", params.get(0).getSchema().getType());

assertEquals("age", params.get(1).getName());
assertEquals("query", params.get(1).getIn());
assertNotNull(params.get(1).getSchema());
assertEquals("integer", params.get(1).getSchema().getType());
assertEquals("int32", params.get(1).getSchema().getFormat());
assertTrue(params.get(1).getSchema().getNullable());
var userIdParam = getParamByName("userId", params);
assertNotNull(userIdParam);
assertEquals("userId", userIdParam.getName());
assertEquals("path", userIdParam.getIn());
assertTrue(userIdParam.getRequired());
assertNotNull(userIdParam.getSchema());
assertEquals("string", userIdParam.getSchema().getType());

var ageParam = getParamByName("age", params);
assertNotNull(ageParam);
assertEquals("age", ageParam.getName());
assertEquals("query", ageParam.getIn());
assertNotNull(ageParam.getSchema());
assertNull(ageParam.getRequired());
assertEquals("integer", ageParam.getSchema().getType());
assertEquals("int32", ageParam.getSchema().getFormat());
assertEquals(123, ageParam.getSchema().getDefault());
assertTrue(ageParam.getSchema().getNullable());
}

private Parameter getParamByName(String name, List<Parameter> params) {
return params.stream()
.filter(p -> name.equals(p.getName()))
.findFirst()
.orElse(null);
}
}
Loading