Skip to content

Commit

Permalink
@withname not correctly applied (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 6, 2021
1 parent f0af955 commit d096923
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -801,12 +801,16 @@ private static boolean appendPropertyName(final MethodVisitor ctor, final Proper

ctor.visitVarInsn(Opcodes.ALOAD, V_STRING_BUILDER);

ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);
if (property.hasPropertyName()) {
ctor.visitLdcInsn(property.getPropertyName());
} else {
ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);

ctor.visitLdcInsn(property.getPropertyName());
ctor.visitLdcInsn(property.getPropertyName());

ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, I_MAPPING_CONTEXT,
"applyNamingStrategy", "(L" + I_STRING + ";)L" + I_STRING + ";", false);
ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, I_MAPPING_CONTEXT,
"applyNamingStrategy", "(L" + I_STRING + ";)L" + I_STRING + ";", false);
}

// stack: sb name
ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, I_STRING_BUILDER, "append",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public static final class CollectionProperty extends MayBeOptionalProperty {
private final Property element;

CollectionProperty(final Class<?> collectionType, final Property element) {
super(element.getMethod(), element.getPropertyName());
super(element.getMethod(), element.hasPropertyName() ? element.getPropertyName() : null);
this.collectionRawType = collectionType;
this.element = element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ private void processEagerGroup(
if (usedProperties.add(memberName)) {
// process by property type
if (!property.isParentPropertyName()) {
NameIterator ni = new NameIterator(namingStrategy.apply(property.getPropertyName()));
NameIterator ni = new NameIterator(property.hasPropertyName() ? property.getPropertyName()
: namingStrategy.apply(property.getPropertyName()));
while (ni.hasNext()) {
currentPath.add(ni.getNextSegment());
ni.next();
Expand Down Expand Up @@ -358,7 +359,8 @@ private void processLazyGroupInGroup(
for (int i = 0; i < pc; i++) {
Property property = group.getProperty(i);
if (!property.isParentPropertyName()) {
NameIterator ni = new NameIterator(namingStrategy.apply(property.getPropertyName()));
NameIterator ni = new NameIterator(property.hasPropertyName() ? property.getPropertyName()
: namingStrategy.apply(property.getPropertyName()));
while (ni.hasNext()) {
currentPath.add(ni.getNextSegment());
ni.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public interface ServerCollectionsOptionals {

Optional<List<Environment>> notRequiredEnvs();

@WithName("req-envs")
@WithName("reqEnvs")
Optional<List<Environment>> requiredEnvs();

interface Environment {
Expand All @@ -214,7 +214,7 @@ interface App {
}

@Test
void mappingCollectionsOptionals() {
void mappingCollectionsOptionals() throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(ServerCollectionsOptionals.class, "server")
.withSources(config(
Expand All @@ -225,8 +225,8 @@ void mappingCollectionsOptionals() {
"server.req.required-services", "rest,db",
"server.req.indexed[0]", "rest",
"server.req.indexed[1]", "db",
"server.req-envs[0].name", "dev",
"server.req-envs[1].name", "prod"))
"server.reqEnvs[0].name", "dev",
"server.reqEnvs[1].name", "prod"))
.build();

ServerCollectionsOptionals server = config.getConfigMapping(ServerCollectionsOptionals.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.microprofile.config.inject.ConfigProperties;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -68,33 +69,36 @@ void composedNamingStrategy() {
.withMapping(ServerComposedVerbatimNaming.class, "server")
.withMapping(ServerComposedKebabNaming.class, "server")
.withSources(config("server.the_host", "localhost", "server.the_port", "8080"))
.withSources(config("server.log.is_enabled", "true", "server.log.log_appenders[0].log_name", "log"))
.withSources(config("server.the_log.is_enabled", "true", "server.the_log.log_appenders[0].log_name", "log"))
.withSources(config("server.theHost", "localhost", "server.thePort", "8080"))
.withSources(config("server.log.isEnabled", "true", "server.log.logAppenders[0].logName", "log"))
.withSources(config("server.oLog.isEnabled", "false", "server.oLog.logAppenders[0].logName", "oLog"))
.withSources(config("server.the-host", "localhost", "server.the-port", "8080"))
.withSources(config("server.log.is-enabled", "true", "server.log.log-appenders[0].log-name", "log"))
.withSources(config("server.the-log.is-enabled", "true", "server.the-log.log-appenders[0].log-name", "log"))
.build();

ServerComposedSnakeNaming snake = config.getConfigMapping(ServerComposedSnakeNaming.class);
assertNotNull(snake);
assertEquals("localhost", snake.theHost());
assertEquals(8080, Integer.valueOf(snake.thePort()));
assertTrue(snake.log().isEnabled());
assertEquals("log", snake.log().logAppenders().get(0).logName());
assertTrue(snake.theLog().isEnabled());
assertEquals("log", snake.theLog().logAppenders().get(0).logName());

ServerComposedVerbatimNaming verbatim = config.getConfigMapping(ServerComposedVerbatimNaming.class);
assertNotNull(verbatim);
assertEquals("localhost", verbatim.theHost());
assertEquals(8080, Integer.valueOf(verbatim.thePort()));
assertTrue(verbatim.log().isEnabled());
assertEquals("log", verbatim.log().logAppenders().get(0).logName());
assertTrue(verbatim.theLog().isEnabled());
assertEquals("log", verbatim.theLog().logAppenders().get(0).logName());
assertTrue(verbatim.optionalLog().isPresent());
assertEquals("oLog", verbatim.optionalLog().get().logAppenders().get(0).logName());

ServerComposedKebabNaming kebab = config.getConfigMapping(ServerComposedKebabNaming.class);
assertNotNull(kebab);
assertEquals("localhost", kebab.theHost());
assertEquals(8080, Integer.valueOf(kebab.thePort()));
assertTrue(kebab.log().isEnabled());
assertEquals("log", kebab.log().logAppenders().get(0).logName());
assertTrue(kebab.theLog().isEnabled());
assertEquals("log", kebab.theLog().logAppenders().get(0).logName());
}

@ConfigMapping(prefix = "server", namingStrategy = ConfigMapping.NamingStrategy.SNAKE_CASE)
Expand All @@ -103,7 +107,7 @@ public interface ServerComposedSnakeNaming {

int thePort();

LogInheritedNaming log();
LogInheritedNaming theLog();
}

@ConfigMapping(prefix = "server", namingStrategy = ConfigMapping.NamingStrategy.VERBATIM)
Expand All @@ -112,7 +116,11 @@ public interface ServerComposedVerbatimNaming {

int thePort();

LogInheritedNaming log();
@WithName("log")
LogInheritedNaming theLog();

@WithName("oLog")
Optional<LogInheritedNaming> optionalLog();
}

@ConfigMapping(prefix = "server")
Expand All @@ -121,7 +129,7 @@ public interface ServerComposedKebabNaming {

int thePort();

LogInheritedNaming log();
LogInheritedNaming theLog();
}

public interface LogInheritedNaming {
Expand Down

0 comments on commit d096923

Please sign in to comment.