Skip to content

Commit

Permalink
fix: handle @ConfigurationProperties nested in @EachProperty properly
Browse files Browse the repository at this point in the history
Apply Qualifier when retrieving nested configuration from interface
annotated with @EachProperty.

Fixes #7210
  • Loading branch information
Paullo612 committed Apr 12, 2022
1 parent 9c3272e commit 8266b4f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Expand Up @@ -30,6 +30,7 @@
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.ReturnType;
import io.micronaut.core.value.PropertyNotFoundException;
import io.micronaut.inject.qualifiers.Qualifiers;

import java.util.Optional;

Expand Down Expand Up @@ -71,16 +72,18 @@ public Object intercept(MethodInvocationContext<Object, Object> context) {
final ReturnType<Object> rt = context.getReturnType();
final Class<Object> returnType = rt.getType();
if (context.isTrue(ConfigurationAdvice.class, MEMBER_BEAN)) {
final Qualifier<Object> qualifier = name != null ? Qualifiers.byName(name) : null;

if (context.isNullable()) {
final Object v = beanContext.findBean(returnType).orElse(null);
final Object v = beanContext.findBean(returnType, qualifier).orElse(null);
if (v != null) {
return environment.convertRequired(v, returnType);
} else {
return v;
}
} else {
return environment.convertRequired(
beanContext.getBean(returnType),
beanContext.getBean(returnType, qualifier),
returnType
);
}
Expand Down
Expand Up @@ -2,8 +2,6 @@ package io.micronaut.inject.foreach

import io.micronaut.context.ApplicationContext
import io.micronaut.inject.qualifiers.Qualifiers
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

class EachPropertyInterfaceSpec extends Specification {
Expand All @@ -30,4 +28,27 @@ class EachPropertyInterfaceSpec extends Specification {
cleanup:
context.close()
}

void "test @EachProperty on interface with nested @ConfigurationProperties"() {
given:
def context = ApplicationContext.run(
"foo.bar[0].address.host": 'host1',
"foo.bar[0].address.port": '9999',
"foo.bar[1].address.host": 'host2',
"foo.bar[1].address.port": '8888'
)

when:
def one = context.getBean(InterfaceAggregatorConfig, Qualifiers.byName("0"))
def two = context.getBean(InterfaceAggregatorConfig, Qualifiers.byName("1"))

then:
one.address.host == 'host1'
one.address.port == 9999
two.address.host == 'host2'
two.address.port == 8888

cleanup:
context.close()
}
}
@@ -0,0 +1,33 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.inject.foreach;

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.context.annotation.EachProperty;

@EachProperty(value = "foo.bar", list = true)
public interface InterfaceAggregatorConfig {

AddressConfig getAddress();

@ConfigurationProperties("address")
interface AddressConfig {

String getHost();

int getPort();
}
}

0 comments on commit 8266b4f

Please sign in to comment.