Class JsonAnnotationPolicyUtils


  • public final class JsonAnnotationPolicyUtils
    extends Object
    Shared resolution/validation logic for the Jackson @JsonInclude/@JsonSetter(nulls = ...) config options and the x-jackson-json-include-policy vendor extension, used by both the spring and kotlin-spring generators (issue #24401).
    • Field Detail

      • OPTIONAL_NON_NULL_PROPERTY_JSON_INCLUDE_DESC

        public static final String OPTIONAL_NON_NULL_PROPERTY_JSON_INCLUDE_DESC
        Shared CliOption description for optionalNonNullPropertyJsonInclude.
        See Also:
        Constant Field Values
      • OPTIONAL_NON_NULL_PROPERTY_JSON_SETTER_NULLS_DESC

        public static final String OPTIONAL_NON_NULL_PROPERTY_JSON_SETTER_NULLS_DESC
        Shared CliOption description for optionalNonNullPropertyJsonSetterNulls.
        See Also:
        Constant Field Values
      • GENERATE_JSON_INCLUDE_ANNOTATIONS_DESC

        public static final String GENERATE_JSON_INCLUDE_ANNOTATIONS_DESC
        Shared CliOption description for generateJsonIncludeAnnotations.
        See Also:
        Constant Field Values
    • Method Detail

      • resolveManualJsonIncludePolicy

        public static String resolveManualJsonIncludePolicy​(Object rawPolicy,
                                                            String extensionName)
        Validate and normalize a manual per-property x-jackson-json-include-policy override.
        Parameters:
        rawPolicy - the raw vendor extension value set directly on the property in the spec
        extensionName - the vendor extension key, used in the error message (kept generator-agnostic)
        Returns:
        the normalized (upper-case) policy name to emit, or null when the override means "emit no annotation" (NONE/blank), in which case the caller must drop the extension.
        Throws:
        IllegalArgumentException - when the override is not a valid JsonInclude.Include value.
      • normalizeJsonIncludePolicy

        public static String normalizeJsonIncludePolicy​(String policy,
                                                        String optionName)
        Validate and normalize the optionalNonNullPropertyJsonInclude config option value.
        Parameters:
        policy - the raw config option value (may be null, in which case the default NON_NULL is returned)
        optionName - the config option name, used in the error message (kept generator-agnostic)
        Returns:
        the normalized (upper-case) policy name: one of NON_NULL, NON_EMPTY, NON_DEFAULT, NONE.
        Throws:
        IllegalArgumentException - when the value is not one of the supported policies.
      • resolveOptionalNonNullPropertyJsonInclude

        public static JsonIncludePolicy resolveOptionalNonNullPropertyJsonInclude​(Map<String,​Object> additionalProperties,
                                                                                  JsonIncludePolicy current)
        Read back the optionalNonNullPropertyJsonInclude config option from additionalProperties, validating/normalizing it via normalizeJsonIncludePolicy(java.lang.String, java.lang.String). Returns current unchanged when the option was not present in additionalProperties (i.e. the generator's existing/default value is kept). Callers are responsible for writing the resolved value back via their own writePropertyBack/additionalProperties.put convention.
        Parameters:
        additionalProperties - the generator's additional properties map
        current - the generator's current optionalNonNullPropertyJsonInclude value
        Returns:
        the resolved JsonIncludePolicy to assign to the generator's field
      • warnIfUnset

        public static void warnIfUnset​(org.slf4j.Logger logger,
                                       TriStateBoolean generateJsonIncludeAnnotations,
                                       TriStateBoolean generateJsonSetterNullsAnnotations)
        Log the shared "option left unset, defaulting to false" warnings for generateJsonIncludeAnnotations/generateJsonSetterNullsAnnotations on the caller's own Logger. No-op for each option that is not TriStateBoolean.UNSET.
        Parameters:
        logger - the calling generator's own LOGGER
        generateJsonIncludeAnnotations - the resolved generateJsonIncludeAnnotations state
        generateJsonSetterNullsAnnotations - the resolved generateJsonSetterNullsAnnotations state
      • resolveJsonIncludePolicy

        public static void resolveJsonIncludePolicy​(CodegenModel model,
                                                    CodegenProperty property,
                                                    TriStateBoolean generateJsonIncludeAnnotations,
                                                    JsonIncludePolicy optionalNonNullPropertyJsonInclude,
                                                    JsonIncludePolicy requiredNonNullablePolicy,
                                                    JsonIncludePolicy requiredNullablePolicy)
        Resolve the @JsonInclude policy into the single universal x-jackson-json-include-policy vendor extension the template emits, mutating model.imports and property.vendorExtensions in place. Precedence:
        1. A value set directly on the property (manual override in the spec) always wins.
        2. Otherwise, when generateJsonIncludeAnnotations=true, apply the automatic matrix:
          • required & non-nullable → requiredNonNullablePolicy
          • required & nullable → requiredNullablePolicy
          • optional & non-nullable → optionalNonNullPropertyJsonInclude (default NON_NULL, NONE = omit)
          • optional & nullable → none (JsonNullable module already governs inclusion)

        The two "required" policies are supplied by the caller since the spring and kotlin-spring generators apply slightly different matrices there: kotlin-spring's non-nullable Kotlin types can never hold null, so it always uses ALWAYS for required properties, whereas spring uses NON_NULL for required-non-nullable (contract protection) and ALWAYS for required-nullable.

        Parameters:
        model - the model owning property, whose imports may be extended
        property - the property to resolve the policy for
        generateJsonIncludeAnnotations - the resolved generateJsonIncludeAnnotations state
        optionalNonNullPropertyJsonInclude - the resolved optionalNonNullPropertyJsonInclude policy
        requiredNonNullablePolicy - the policy to apply to required, non-nullable properties
        requiredNullablePolicy - the policy to apply to required, nullable properties
      • resolveJsonSetterNullsMode

        public static JsonAnnotationPolicyUtils.JsonSetterNullsMode resolveJsonSetterNullsMode​(TriStateBoolean generateJsonSetterNullsAnnotations,
                                                                                               boolean required,
                                                                                               boolean nullable,
                                                                                               boolean openApiNullable,
                                                                                               boolean failModeSupported,
                                                                                               JsonAnnotationPolicyUtils.JsonSetterNullsMode optionalNonNullOverride)
        Resolve which @JsonSetter(nulls = ...) annotation, if any, should be emitted for an optional, non-nullable property, honoring the optionalNonNullPropertyJsonSetterNulls global override.
        Parameters:
        generateJsonSetterNullsAnnotations - the resolved generateJsonSetterNullsAnnotations state
        required - whether the property is required
        nullable - whether the property is nullable
        openApiNullable - whether the generator's openApiNullable option is enabled
        failModeSupported - whether this generator's default path emits JsonAnnotationPolicyUtils.JsonSetterNullsMode.FAIL (kotlin-spring does; spring's default path never does)
        optionalNonNullOverride - the resolved optionalNonNullPropertyJsonSetterNulls option (null when unset); when set it wins over the openApiNullable default and is emitted regardless of failModeSupported
        Returns:
        the mode to emit for an optional non-nullable property, or JsonAnnotationPolicyUtils.JsonSetterNullsMode.NONE
      • warnIfJsonSetterNullsDefaultRisky

        public static void warnIfJsonSetterNullsDefaultRisky​(org.slf4j.Logger logger,
                                                             TriStateBoolean generateJsonSetterNullsAnnotations,
                                                             JsonAnnotationPolicyUtils.JsonSetterNullsMode optionalNonNullOverride,
                                                             boolean openApiNullable,
                                                             boolean failModeSupported)
        Log a one-time warning when the generator falls back to the (technically risky) "no @JsonSetter" default for optional non-nullable properties: generateJsonSetterNullsAnnotations=true, no optionalNonNullPropertyJsonSetterNulls override, openApiNullable=true, and the generator's default path does not emit FAIL (failModeSupported=false, i.e. spring/java). In that case an explicit JSON null falls back to the global mapper default (Nulls.SET) and overwrites field defaults. kotlin-spring passes failModeSupported=true and stays silent.