Skip to content

Commit 3f13319

Browse files
authored
Merge pull request #1015 from SageDroid/fix/1014
Apply `excludeFields` only to fields in `Library`
2 parents 3b2ff20 + 98eef96 commit 3f13319

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ aboutLibraries {
118118
// Full license text for license IDs mentioned here will be included, even if no detected dependency uses them.
119119
additionalLicenses = ["mit", "mpl_2_0"]
120120
// Allows to exclude some fields from the generated meta data field.
121-
excludeFields = ["developers", "funding"]
121+
// If the class name is specified, the field is only excluded for that class; without a class name, the exclusion is global.
122+
excludeFields = ["License.name", "developers", "funding"]
122123
// Enable inclusion of `platform` dependencies in the library report
123124
includePlatform = true
124125
// Define the strict mode, will fail if the project uses licenses not allowed

plugin-build/plugin/src/main/kotlin/com/mikepenz/aboutlibraries/plugin/AboutLibrariesExtension.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,14 @@ abstract class AboutLibrariesExtension {
219219
/**
220220
* Defines fields which will be excluded during the serialisation of the metadata output file.
221221
*
222-
* Any field as included in the [com.mikepenz.aboutlibraries.plugin.mapping.Library] can theoretically be excluded.
222+
* It is possible to qualify the field names by specifying the class name (e.g. "License.name").
223+
* Permissible qualifiers are "ResultContainer", "Library", "Developer", "Organization", "Funding", "Scm",
224+
* "License" and "MetaData".
225+
* Unqualified field names (e.g. "description") are applied to the entire output.
226+
*
223227
* ```
224228
* aboutLibraries {
225-
* excludeFields = arrayOf("description", "tag")
229+
* excludeFields = arrayOf("License.name", "ResultContainer.metadata", "description", "tag")
226230
* }
227231
* ```
228232
*/

plugin-build/plugin/src/main/kotlin/com/mikepenz/aboutlibraries/plugin/model/ResultContainer.kt

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.mikepenz.aboutlibraries.plugin.model
22

3+
import com.mikepenz.aboutlibraries.plugin.mapping.Developer
4+
import com.mikepenz.aboutlibraries.plugin.mapping.Funding
35
import com.mikepenz.aboutlibraries.plugin.mapping.Library
46
import com.mikepenz.aboutlibraries.plugin.mapping.License
7+
import com.mikepenz.aboutlibraries.plugin.mapping.Organization
8+
import com.mikepenz.aboutlibraries.plugin.mapping.Scm
9+
import com.mikepenz.aboutlibraries.plugin.util.PartialObjectConverter
510
import groovy.json.JsonGenerator
611
import groovy.json.JsonOutput
712
import java.io.File
@@ -26,10 +31,35 @@ class MetaData(
2631
)
2732

2833
fun ResultContainer.writeToDisk(outputFile: File, excludeFields: Array<String>, prettyPrint: Boolean) {
29-
val fieldNames = mutableListOf("artifactId", "groupId", "artifactFolder").also {
30-
it.addAll(excludeFields)
34+
val allowedExclusionQualifiers = setOf(
35+
ResultContainer::class.simpleName,
36+
Library::class.simpleName,
37+
Developer::class.simpleName,
38+
Organization::class.simpleName,
39+
Funding::class.simpleName,
40+
Scm::class.simpleName,
41+
License::class.simpleName,
42+
MetaData::class.simpleName,
43+
)
44+
val excludedQualifiedFieldNames = mutableSetOf(
45+
"${Library::class.simpleName}.${Library::artifactId.name}",
46+
"${Library::class.simpleName}.${Library::groupId.name}",
47+
"${Library::class.simpleName}.${Library::artifactFolder.name}"
48+
)
49+
val excludedUnqualifiedFieldNames = mutableSetOf<String>()
50+
excludeFields.forEach { excludedField ->
51+
val segments = excludedField.split(".")
52+
if (segments.size == 2 && allowedExclusionQualifiers.contains(segments.first())) {
53+
excludedQualifiedFieldNames.add(excludedField)
54+
} else {
55+
excludedUnqualifiedFieldNames.add(excludedField)
56+
}
3157
}
32-
val jsonGenerator = JsonGenerator.Options().excludeNulls().excludeFieldsByName(fieldNames).build()
58+
val jsonGenerator = JsonGenerator.Options()
59+
.excludeNulls()
60+
.excludeFieldsByName(excludedUnqualifiedFieldNames)
61+
.addConverter(PartialObjectConverter(excludedQualifiedFieldNames))
62+
.build()
3363
PrintWriter(OutputStreamWriter(outputFile.outputStream(), StandardCharsets.UTF_8), true).use {
3464
it.write(jsonGenerator.toJson(this).let { json -> if (prettyPrint) JsonOutput.prettyPrint(json) else json })
3565
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mikepenz.aboutlibraries.plugin.util
2+
3+
import groovy.json.DefaultJsonGenerator
4+
import groovy.json.JsonGenerator
5+
import org.codehaus.groovy.runtime.DefaultGroovyMethods
6+
7+
/**
8+
* A converter for [JsonGenerator], which allows properties to be excluded from the output.
9+
* The way it works is identical to the serialization of objects in [DefaultJsonGenerator].
10+
* @property excludedQualifiedPropertyNames The qualified name (class name + property name)
11+
* of the properties that should be excluded from serialization.
12+
*/
13+
class PartialObjectConverter(
14+
private val excludedQualifiedPropertyNames: Set<String>
15+
) : JsonGenerator.Converter {
16+
17+
private val targetClassNames: Set<String> = excludedQualifiedPropertyNames.mapTo(mutableSetOf()) { field ->
18+
field.substringBeforeLast('.')
19+
}
20+
21+
private val excludedPropertyNames = setOf("class", "declaringClass", "metaClass")
22+
23+
override fun handles(type: Class<*>?): Boolean {
24+
return type != null && targetClassNames.contains(type.simpleName)
25+
}
26+
27+
override fun convert(value: Any, key: String?): Any {
28+
return DefaultGroovyMethods.getProperties(value).filterKeys { propertyName ->
29+
propertyName !in excludedPropertyNames && "${value::class.simpleName}.$propertyName" !in excludedQualifiedPropertyNames
30+
}
31+
}
32+
33+
}

0 commit comments

Comments
 (0)