Skip to content

Add SpEL support for default values #1317

Closed
@ThanksForAllTheFish

Description

@ThanksForAllTheFish

With a simple application like

package com.example.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
	runApplication<DemoApplication>(*args)
}

@RestController
@RequestMapping("/test")
class TestController {
	@GetMapping
	fun test(@RequestParam(defaultValue = "#{{}}") list: List<String>) =
		list
}

and a build.gradle.kts file autogenerated from Spring Initializr plus springdoc addition as follow

implementation("org.springdoc:springdoc-openapi-ui:1.5.12")
implementation("org.springdoc:springdoc-openapi-kotlin:1.5.12")
implementation("org.springdoc:springdoc-openapi-hateoas:1.5.12")

I get the following OpenAPI definition:

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenAPI definition",
    "version": "v0"
  },
  "servers": [
    {
      "url": "http://localhost:8080",
      "description": "Generated server url"
    }
  ],
  "paths": {
    "/test": {
      "get": {
        "tags": [
          "test-controller"
        ],
        "operationId": "test",
        "parameters": [
          {
            "name": "list",
            "in": "query",
            "required": false,
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "default": [
                "#{{}}"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "*/*": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

In particular, "default": ["#{{}}"] struck me as wrong, since fun test(@RequestParam(defaultValue = "#{{}}") list: List<String>) actually uses a SpEL expression which should convert to an empty list. Should this work out of the box or am I missing some enabler configuration?

Changing to fun test(@RequestParam(defaultValue = "#{{}}") list: List<Int>) gets even worse, as now the conversion service throws an exception that translates into org.springdoc.core.ParameterInfo : Using the following default value : #{{}}, without spring conversionService

Sample project: https://github.com/ThanksForAllTheFish/springdocspel

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions