Skip to content

Add SpEL support for default values #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ThanksForAllTheFish opened this issue Oct 21, 2021 · 0 comments
Closed

Add SpEL support for default values #1317

ThanksForAllTheFish opened this issue Oct 21, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@ThanksForAllTheFish
Copy link

ThanksForAllTheFish commented Oct 21, 2021

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

@bnasslahsen bnasslahsen changed the title SpEL does not seem to be supported for default values Add SpEL support for default values Oct 23, 2021
@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
mpleine pushed a commit to mpleine/springdoc-openapi that referenced this issue May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants