codeconsole commented on code in PR #16237: URL: https://github.com/apache/grails-core/pull/16237#discussion_r4249267608
########## grails-converters/src/main/groovy/org/grails/web/converters/jackson/JsonProjection.java: ########## @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.grails.web.converters.jackson; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tools.jackson.databind.BeanDescription; +import tools.jackson.databind.JacksonModule; +import tools.jackson.databind.SerializationConfig; +import tools.jackson.databind.introspect.BeanPropertyDefinition; +import tools.jackson.databind.json.JsonMapper; +import tools.jackson.databind.module.SimpleModule; +import tools.jackson.databind.ser.BeanPropertyWriter; +import tools.jackson.databind.ser.ValueSerializerModifier; + +import org.springframework.beans.BeanUtils; + +/** + * A per-write {@code includes}/{@code excludes} projection, as the {@code render} and {@code respond} + * arguments request it. + * + * <p>As with the legacy converter, it applies to the properties of objects of the written value's + * type, or of its elements' types when the value is a collection or an array. It applies to domain + * objects and to beans Jackson writes property by property. Values with no properties to project, + * such as maps and strings, are rejected before anything is written.</p> + * + * @since 9.0 + */ +public final class JsonProjection { + + /** Writer attribute holding the projection of one write. */ + public static final String ATTRIBUTE = JsonProjection.class.getName(); + + private static final Logger LOG = LoggerFactory.getLogger(JsonProjection.class); + + private final List<String> includes; + private final List<String> excludes; + private final Set<Class<?>> types; + private final Set<Class<?>> applied = ConcurrentHashMap.newKeySet(); + + private JsonProjection(List<String> includes, List<String> excludes, Set<Class<?>> types) { + this.includes = includes; + this.excludes = excludes; + this.types = types; + } + + /** + * @param value the value to be written + * @param includes property names to include, or null for all + * @param excludes property names to exclude, or null for none + * @return the projection, or null if neither list names a property + * @throws IllegalArgumentException if the value, or an element of it, has no properties to project + */ + public static JsonProjection of(Object value, List<String> includes, List<String> excludes) { + boolean including = includes != null && !includes.isEmpty(); + boolean excluding = excludes != null && !excludes.isEmpty(); + if (!including && !excluding) { + return null; + } + Set<Class<?>> types = new LinkedHashSet<>(); + if (value instanceof Iterable<?> elements) { Review Comment: Fixed in eb2e6d63cc. Only `Collection`s and arrays are walked. For any other `Iterable` the elements are written unprojected, and `reportUnapplied` warns that they were not projected. `NamedJsonConfigurationRegistrySpec` writes a single-pass iterable and gets its element back instead of `[]`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
