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

fix: handle @ConfigurationProperties nested in @EachProperty properly #7211

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}