Skip to content

Commit

Permalink
fix(core): Multiple use of nested configuration results in multiple p…
Browse files Browse the repository at this point in the history
…roperties (#71) (#73)

- Multiple nested properties with the same base prefix was being collected under each other, now properties are only being put inside a property group if the properties start with the groups prefix, of course the unknown group is an exception
  • Loading branch information
nandorholozsnyak authored Jul 6, 2023
1 parent ccfb189 commit e5a9a8f
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 167 deletions.
5 changes: 3 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
end_of_line = lf
indent_size = 4
indent_style = space
ij_java_class_count_to_use_import_on_demand = 999
ij_java_names_count_to_use_import_on_demand = 999
max_line_length = 140
ij_java_class_count_to_use_import_on_demand = 500
ij_java_names_count_to_use_import_on_demand = 500
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<maven-plugin-report-plugin.version>3.8.1</maven-plugin-report-plugin.version>
<itf.version>0.12.0</itf.version>
<build-helper-maven-plugin.version>3.3.0</build-helper-maven-plugin.version>
<junit-pioneer.version>2.0.0</junit-pioneer.version>
</properties>

<name>Spring Configuration Property Documenter</name>
Expand Down Expand Up @@ -282,6 +283,11 @@
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter-params.version}</version>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>${junit-pioneer.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions spring-configuration-property-documenter-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,9 @@ public List<PropertyGroup> readPropertiesAsPropertyGroupList(InputStream metadat
}
}

private PropertyGroup setProperties(Map<String, List<Property>> propertyMap, PropertyGroup propertyGroup) {
List<Property> properties = propertyMap.get(propertyGroup.getType());
if (properties == null || properties.isEmpty()) {
LOGGER.warn("Property group with name:[{}] is having no properties, please check if you provided the getter/setter methods. If your class is empty intentionally, please forget this warning here.", propertyGroup.getGroupName());
return propertyGroup;
}
List<Property> collectedProperties = properties.stream()
.map(property -> updateProperty(propertyGroup, property))
.collect(Collectors.toList());
propertyGroup.setProperties(collectedProperties);
return propertyGroup;
}

private Property updateProperty(PropertyGroup propertyGroup, Property property) {
String groupName = propertyGroup.getGroupName();
if(propertyGroup.isUnknownGroup()) {
if (propertyGroup.isUnknownGroup()) {
property.setKey(property.getFqName());
} else {
property.setKey(property.getFqName().substring(groupName.length() + 1));
Expand All @@ -103,9 +90,9 @@ private Property updateProperty(PropertyGroup propertyGroup, Property property)

private List<PropertyGroup> flattenValues(Map<String, List<PropertyGroup>> propertyGroupsByType) {
return propertyGroupsByType.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

private void updateGroupsWithPropertiesAndAssociations(Map<String, List<Property>> propertyMap, Map<String, List<PropertyGroup>> propertyGroupsByType) {
Expand All @@ -123,18 +110,32 @@ private void updateGroupsWithPropertiesAndAssociations(Map<String, List<Property

private List<PropertyGroup> updatePropertiesAndReturnNestedGroups(Map<String, List<Property>> propertyMap, Map.Entry<String, List<PropertyGroup>> propertyEntry) {
return propertyEntry.getValue()
.stream()
.map(propertyGroup -> setProperties(propertyMap, propertyGroup))
.filter(PropertyGroup::isNested)
.collect(Collectors.toList());
.stream()
.map(propertyGroup -> setProperties(propertyMap, propertyGroup))
.filter(PropertyGroup::isNested)
.collect(Collectors.toList());
}

private PropertyGroup setProperties(Map<String, List<Property>> propertyMap, PropertyGroup propertyGroup) {
List<Property> properties = propertyMap.get(propertyGroup.getType());
if (properties == null || properties.isEmpty()) {
LOGGER.warn("Property group with name:[{}] is having no properties, please check if you provided the getter/setter methods. If your class is empty intentionally, please forget this warning here.", propertyGroup.getGroupName());
return propertyGroup;
}
List<Property> collectedProperties = properties.stream()
.filter(property -> property.getFqName().startsWith(propertyGroup.getGroupName()) || propertyGroup.isUnknownGroup())
.map(property -> updateProperty(propertyGroup, property))
.collect(Collectors.toList());
propertyGroup.setProperties(collectedProperties);
return propertyGroup;
}

private Map<String, List<PropertyGroup>> getPropertyGroups(ConfigurationMetadata configurationMetadata) {
Map<String, List<PropertyGroup>> propertyGroupMap = configurationMetadata.getItems()
.stream()
.filter(itemMetadata -> itemMetadata.isOfItemType(ItemMetadata.ItemType.GROUP))
.map(itemMetadata -> new PropertyGroup(itemMetadata.getName(), itemMetadata.getType(), getSourceTypeOrDefault(itemMetadata)))
.collect(Collectors.groupingBy(PropertyGroup::getSourceType, Collectors.toList()));
.stream()
.filter(itemMetadata -> itemMetadata.isOfItemType(ItemMetadata.ItemType.GROUP))
.map(itemMetadata -> new PropertyGroup(itemMetadata.getName(), itemMetadata.getType(), getSourceTypeOrDefault(itemMetadata)))
.collect(Collectors.groupingBy(PropertyGroup::getSourceType, Collectors.toList()));
List<PropertyGroup> value = new ArrayList<>();
value.add(PropertyGroup.createUnknownGroup());
propertyGroupMap.put(PropertyGroupConstants.UNKNOWN, value);
Expand All @@ -144,11 +145,11 @@ private Map<String, List<PropertyGroup>> getPropertyGroups(ConfigurationMetadata
private Map<String, List<Property>> getPropertyMap(ConfigurationMetadata configurationMetadata) {
Function<ItemMetadata, String> getSourceType = this::getSourceTypeOrDefault;
return configurationMetadata.getItems()
.stream()
.filter(itemMetadata -> itemMetadata.isOfItemType(ItemMetadata.ItemType.PROPERTY))
.collect(Collectors.groupingBy(getSourceType,
Collectors.mapping(this::mapToProperty, Collectors.toList()))
);
.stream()
.filter(itemMetadata -> itemMetadata.isOfItemType(ItemMetadata.ItemType.PROPERTY))
.collect(Collectors.groupingBy(getSourceType,
Collectors.mapping(this::mapToProperty, Collectors.toList()))
);
}

private String getSourceTypeOrDefault(ItemMetadata current) {
Expand Down
Loading

0 comments on commit e5a9a8f

Please sign in to comment.