Fix null defaults for Spring query parameters

DM
12h ago
+131-0

Fixes #25463

Problem

The source contract has an optional array query parameter with no default. The openapi-yaml bundling pass emits default: null for that array schema; feeding the bundled file to Spring generation can produce @RequestParam(..., defaultValue = "null"). Spring then treats an omitted query parameter as if the caller supplied the literal null.

Root cause

The bundled document represents the default as a Jackson NullNode. Java parameter-default conversion treats this non-Java-null object as a value and stringifies it. The Spring query-parameter template then writes the resulting string into the annotation.

Solution

SpringCodegen treats a Jackson null node as having no parameter default, while delegating all other values to the existing conversion. The change is scoped to Spring and retains legitimate defaults, including a string default whose value is literally "null".

Regression tests

Added an OpenAPI 3.1 fixture representing the bundled document and a generator test using spring-http-interface. It checks the null default, an optional string without a default, an integer default, a required array, an explicit array default, and a literal string default of "null".

The regression test failed before the change with defaultValue = "null" on ids and passes after it. Separately, running openapi-yaml 7.25.0 and 7.26.0 on a minimal source specification with no array default reproduces the intermediate default: null value.

Compatibility

No global DefaultCodegen behavior or Mustache templates changed. Non-null explicit defaults are handled by the existing Spring conversion. Other generators are unaffected.

Validation

  • mvn -pl modules/openapi-generator -am '-Dtest=SpringCodegenTest#nullSchemaDefaultIsNotRenderedAsSpringRequestParamDefault' '-Dsurefire.failIfNoSpecifiedTests=false' test — passed (1 test).
  • mvn -pl modules/openapi-generator -am '-Dtest=SpringCodegenTest' '-Dsurefire.failIfNoSpecifiedTests=false' test — passed (487 tests, 0 failures, 0 errors).
  • git diff --check — passed.

The targeted regression test failed before the fix, confirming the generated annotation regression.


Summary by cubic

Fixes the Spring generator emitting defaultValue = "null" for optional query parameters whose schema has default: null, which made Spring treat an omitted parameter as the literal string null.

SpringCodegen now treats a Jackson null node as no default at all, while all other defaults keep the existing conversion behavior. For $ref schemas, the parameter's own explicit default is preserved even when the referenced schema declares default: null, and a referenced schema's null default with no override is ignored. Added a regression test covering null defaults, optional strings, numeric defaults, required arrays, explicit array defaults, literal "null" string defaults, and $ref schemas with and without overridden defaults.

  • Bug Fixes
    • Null JsonNode defaults, including those inherited from $ref schemas, are no longer stringified into Spring @RequestParam annotations.
    • Non-null explicit defaults and literal string "null" values are preserved, including overrides on $ref schemas.