|
| 1 | +/* |
| 2 | + * Copyright 2026-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.agentclientprotocol.sdk.json; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.Version; |
| 8 | + |
| 9 | +/** |
| 10 | + * The Jackson 2 versions acp-json-jackson2 supports: {@value #FLOOR} or later, for jackson-core |
| 11 | + * and jackson-databind alike. Frameworks manage their own Jackson version, and an older |
| 12 | + * jackson-core under a newer jackson-databind fails much later with a {@code NoSuchMethodError} |
| 13 | + * while reading a message; checking when the mapper is created names the problem instead. |
| 14 | + */ |
| 15 | +final class JacksonVersions { |
| 16 | + |
| 17 | + /** |
| 18 | + * The oldest Jackson 2 release the SDK's tests pass with. 2.18.0 loses unknown values of the |
| 19 | + * SDK's open enumerations (stop reasons, plan entry statuses), and 2.17 the unknown fields the |
| 20 | + * SDK keeps for forward compatibility. |
| 21 | + */ |
| 22 | + static final String FLOOR = "2.18.1"; |
| 23 | + |
| 24 | + private static final Version FLOOR_VERSION = new Version(2, 18, 1, null, "com.fasterxml.jackson.core", |
| 25 | + "jackson-core"); |
| 26 | + |
| 27 | + private JacksonVersions() { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Fails unless the jackson-core and jackson-databind on the classpath are {@value #FLOOR} or |
| 32 | + * later. |
| 33 | + * @throws IllegalStateException naming the version found and the version required |
| 34 | + */ |
| 35 | + static void requireSupported() { |
| 36 | + require("jackson-core", com.fasterxml.jackson.core.json.PackageVersion.VERSION); |
| 37 | + require("jackson-databind", com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Fails unless {@code found} is {@value #FLOOR} or later. |
| 42 | + * @param artifact the Jackson artifact the version belongs to |
| 43 | + * @param found the version on the classpath |
| 44 | + * @throws IllegalStateException naming the version found and the version required |
| 45 | + */ |
| 46 | + static void require(String artifact, Version found) { |
| 47 | + if (found.getMajorVersion() != FLOOR_VERSION.getMajorVersion() |
| 48 | + || compare(found, FLOOR_VERSION) < 0) { |
| 49 | + throw new IllegalStateException("acp-json-jackson2 needs Jackson " + FLOOR + " or later, but " + artifact |
| 50 | + + " " + found.toString() + " is on the classpath (jackson-core " |
| 51 | + + com.fasterxml.jackson.core.json.PackageVersion.VERSION.toString() + ", jackson-databind " |
| 52 | + + com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION.toString() |
| 53 | + + "). Align the Jackson 2 artifacts on one version, " + FLOOR |
| 54 | + + " or later, for example by importing com.fasterxml.jackson:jackson-bom."); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** Compares major, minor and patch level only, ignoring the artifact and any snapshot. */ |
| 59 | + private static int compare(Version a, Version b) { |
| 60 | + int major = Integer.compare(a.getMajorVersion(), b.getMajorVersion()); |
| 61 | + if (major != 0) { |
| 62 | + return major; |
| 63 | + } |
| 64 | + int minor = Integer.compare(a.getMinorVersion(), b.getMinorVersion()); |
| 65 | + return (minor != 0) ? minor : Integer.compare(a.getPatchLevel(), b.getPatchLevel()); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments