Skip to content

Commit

Permalink
Merge pull request #1015 from SageDroid/fix/1014
Browse files Browse the repository at this point in the history
Apply `excludeFields` only to fields in `Library`
  • Loading branch information
mikepenz committed Aug 23, 2024
2 parents 3b2ff20 + 98eef96 commit 3f13319
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ aboutLibraries {
// Full license text for license IDs mentioned here will be included, even if no detected dependency uses them.
additionalLicenses = ["mit", "mpl_2_0"]
// Allows to exclude some fields from the generated meta data field.
excludeFields = ["developers", "funding"]
// If the class name is specified, the field is only excluded for that class; without a class name, the exclusion is global.
excludeFields = ["License.name", "developers", "funding"]
// Enable inclusion of `platform` dependencies in the library report
includePlatform = true
// Define the strict mode, will fail if the project uses licenses not allowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,14 @@ abstract class AboutLibrariesExtension {
/**
* Defines fields which will be excluded during the serialisation of the metadata output file.
*
* Any field as included in the [com.mikepenz.aboutlibraries.plugin.mapping.Library] can theoretically be excluded.
* It is possible to qualify the field names by specifying the class name (e.g. "License.name").
* Permissible qualifiers are "ResultContainer", "Library", "Developer", "Organization", "Funding", "Scm",
* "License" and "MetaData".
* Unqualified field names (e.g. "description") are applied to the entire output.
*
* ```
* aboutLibraries {
* excludeFields = arrayOf("description", "tag")
* excludeFields = arrayOf("License.name", "ResultContainer.metadata", "description", "tag")
* }
* ```
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.mikepenz.aboutlibraries.plugin.model

import com.mikepenz.aboutlibraries.plugin.mapping.Developer
import com.mikepenz.aboutlibraries.plugin.mapping.Funding
import com.mikepenz.aboutlibraries.plugin.mapping.Library
import com.mikepenz.aboutlibraries.plugin.mapping.License
import com.mikepenz.aboutlibraries.plugin.mapping.Organization
import com.mikepenz.aboutlibraries.plugin.mapping.Scm
import com.mikepenz.aboutlibraries.plugin.util.PartialObjectConverter
import groovy.json.JsonGenerator
import groovy.json.JsonOutput
import java.io.File
Expand All @@ -26,10 +31,35 @@ class MetaData(
)

fun ResultContainer.writeToDisk(outputFile: File, excludeFields: Array<String>, prettyPrint: Boolean) {
val fieldNames = mutableListOf("artifactId", "groupId", "artifactFolder").also {
it.addAll(excludeFields)
val allowedExclusionQualifiers = setOf(
ResultContainer::class.simpleName,
Library::class.simpleName,
Developer::class.simpleName,
Organization::class.simpleName,
Funding::class.simpleName,
Scm::class.simpleName,
License::class.simpleName,
MetaData::class.simpleName,
)
val excludedQualifiedFieldNames = mutableSetOf(
"${Library::class.simpleName}.${Library::artifactId.name}",
"${Library::class.simpleName}.${Library::groupId.name}",
"${Library::class.simpleName}.${Library::artifactFolder.name}"
)
val excludedUnqualifiedFieldNames = mutableSetOf<String>()
excludeFields.forEach { excludedField ->
val segments = excludedField.split(".")
if (segments.size == 2 && allowedExclusionQualifiers.contains(segments.first())) {
excludedQualifiedFieldNames.add(excludedField)
} else {
excludedUnqualifiedFieldNames.add(excludedField)
}
}
val jsonGenerator = JsonGenerator.Options().excludeNulls().excludeFieldsByName(fieldNames).build()
val jsonGenerator = JsonGenerator.Options()
.excludeNulls()
.excludeFieldsByName(excludedUnqualifiedFieldNames)
.addConverter(PartialObjectConverter(excludedQualifiedFieldNames))
.build()
PrintWriter(OutputStreamWriter(outputFile.outputStream(), StandardCharsets.UTF_8), true).use {
it.write(jsonGenerator.toJson(this).let { json -> if (prettyPrint) JsonOutput.prettyPrint(json) else json })
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mikepenz.aboutlibraries.plugin.util

import groovy.json.DefaultJsonGenerator
import groovy.json.JsonGenerator
import org.codehaus.groovy.runtime.DefaultGroovyMethods

/**
* A converter for [JsonGenerator], which allows properties to be excluded from the output.
* The way it works is identical to the serialization of objects in [DefaultJsonGenerator].
* @property excludedQualifiedPropertyNames The qualified name (class name + property name)
* of the properties that should be excluded from serialization.
*/
class PartialObjectConverter(
private val excludedQualifiedPropertyNames: Set<String>
) : JsonGenerator.Converter {

private val targetClassNames: Set<String> = excludedQualifiedPropertyNames.mapTo(mutableSetOf()) { field ->
field.substringBeforeLast('.')
}

private val excludedPropertyNames = setOf("class", "declaringClass", "metaClass")

override fun handles(type: Class<*>?): Boolean {
return type != null && targetClassNames.contains(type.simpleName)
}

override fun convert(value: Any, key: String?): Any {
return DefaultGroovyMethods.getProperties(value).filterKeys { propertyName ->
propertyName !in excludedPropertyNames && "${value::class.simpleName}.$propertyName" !in excludedQualifiedPropertyNames
}
}

}

0 comments on commit 3f13319

Please sign in to comment.