From 79cd01eb7a86134748ef2a84c7978214974d01a3 Mon Sep 17 00:00:00 2001 From: Eric Bottard Date: Thu, 3 Mar 2016 18:32:04 +0100 Subject: [PATCH] Fix configuration property name when group is empty See gh-5335 --- .../RawConfigurationMetadata.java | 6 +++--- .../configurationmetadata/JsonReaderTests.java | 13 ++++++++++++- .../configuration-metadata-emptygroup.json | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-emptygroup.json diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java index ecb10a883a..0533776ffa 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java @@ -77,10 +77,10 @@ class RawConfigurationMetadata { } ConfigurationMetadataSource source = getSource(item.getSourceType()); if (source != null) { - String groupId = source.getGroupId(); + String dottedPrefix = source.getGroupId() + "."; String id = item.getId(); - if (id.startsWith(groupId)) { // match - String name = id.substring(groupId.length() + 1, id.length()); // "." + if (id.startsWith(dottedPrefix)) { + String name = id.substring(dottedPrefix.length(), id.length()); item.setName(name); } } diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java index ce75549d21..f286ab7fe8 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,6 +52,17 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests { readFor("invalid"); } + @Test + public void emptyGroupName() throws IOException { + RawConfigurationMetadata rawMetadata = readFor("emptygroup"); + List items = rawMetadata.getItems(); + assertEquals(1, items.size()); + + ConfigurationMetadataItem item = items.get(0); + assertProperty(item, "name", "name", String.class, null); + + } + @Test public void simpleMetadata() throws IOException { RawConfigurationMetadata rawMetadata = readFor("foo"); diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-emptygroup.json b/spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-emptygroup.json new file mode 100644 index 0000000000..eb16f3cb44 --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-emptygroup.json @@ -0,0 +1,18 @@ +{ + "groups": [ + { + "name": "", + "type": "org.acme.Foo", + "sourceType": "org.acme.config.FooApp", + "sourceMethod": "foo()", + "description": "This is Foo." + } + ], + "properties": [ + { + "name": "name", + "type": "java.lang.String", + "sourceType": "org.acme.Foo" + } + ] +}