JsonMapper rejects JSON arrays over 1000 elements: hardcoded limit, silent failure in Spring Cloud Stream

Ouverte
#1,538 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
55/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
java, kafka, spring-boot
Domaine
backend

Piste de recherche

Start with the linked upstream commit, then read ContextFunctionCatalogAutoConfiguration.JsonMapperConfiguration#jackson, which unconditionally registers CollectionLimitModule, and LimitedCollectionDeserializer where MAX_ITEMS = 1000 is hardcoded. From there follow JsonMessageConverter#convertFromInternal, which swallows the failure at DEBUG and returns null. A fix means a configurable bound (with an off switch), a WARN or MessageConversionException instead of null, and tests reproducing the 1001-element Payload sample.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Describe the bug

Since 5.1.0 (and 5.0.6), the auto-configured Jackson JsonMapper rejects any JSON array with more than 1000 elements. The limit is hardcoded, and in Spring Cloud Stream the failure is silent, so it is hard to find.

Versions: Spring Cloud Function 5.1.0, Spring Cloud Stream 5.1.0 with the Kafka binder, Spring Boot 4.2.0, Jackson 3.3.0.

Where it happens:

  • ContextFunctionCatalogAutoConfiguration.JsonMapperConfiguration#jackson always registers CollectionLimitModule.
  • LimitedCollectionDeserializer throws IllegalStateException: Collection size exceeds limit of 1000 when MAX_ITEMS = 1000 is exceeded. There is no property to change or disable it.
  • JsonMessageConverter#convertFromInternal catches the exception, logs it only at DEBUG and returns null.
  • The function then receives the raw byte[] payload and fails with ClassCastException: class [B cannot be cast to class MyDto. In our setup the message then goes to the DLQ after retries.

This hit us in production after upgrading to Spring Boot 4.2. One of our consumers receives a valid JSON document with a list of about 3700 items. It worked before the upgrade and fails with 5.1.0. The only visible error was the ClassCastException, which points away from the real cause.

The upstream change that added the limit: Ensure deserialization of JSON arrays/collections is bounded.

We understand why a bound is useful. Would it be possible to:

  1. Make the limit configurable, for example with a spring.cloud.function.* property, including a way to turn it off.
  2. Report the conversion failure at WARN level or as a MessageConversionException, instead of returning null and letting the function fail with a ClassCastException.

As a workaround we can declare our own org.springframework.cloud.function.json.JsonMapper bean, since the auto-configured one is @ConditionalOnMissingBean. That also drops the other defaults of the auto-configured mapper, so a property would be much simpler.

Sample
record Payload(List<Integer> items) {}

@Autowired
org.springframework.cloud.function.json.JsonMapper jsonMapper;

// JSON with 1001 elements: {"items":[0,1,2,...,1000]}
jsonMapper.fromJson(json.getBytes(StandardCharsets.UTF_8), Payload.class);
// tools.jackson.databind.DatabindException: Collection size exceeds limit of 1000

In a stream application, a Consumer<Payload> bean bound to a Kafka topic shows the same problem as a ClassCastException.

Thank you!

Langage dominant
Java
Étoiles
1.1k
Forks
655
Merge moyen
4 h 54 min
PR mergées (30 j)
10

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de spring-cloud/spring-cloud-function

Toutes les issues de spring-cloud/spring-cloud-function

Issues similaires

Plus d'issues Java

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.