Skip to content
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

Exceptions annoted with ResponseStatus not converted to Problem on 0.26.x when using Spring Security #541

Open
timmhirsens opened this issue Sep 29, 2020 · 5 comments

Comments

@timmhirsens
Copy link

timmhirsens commented Sep 29, 2020

Exceptions that are annoted with a @ResponseStatus do not return a "Problem Response" with content-type application/problem+json once the Spring Security Starter is on the classpath.

This error does not appear when using version 0.25.2 only when updating to a version >= 0.26.0

Description

The autoconfiguration is invoked, but the method still returns application/json and the default spring boot error json:

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpStatus
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class ProblemStarterWebIssueApplication

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

@RestController
@RequestMapping("/hello")
class HelloWorldController {
    @GetMapping
    fun sayHello() {
        throw HelloNotFoundException()
    }
}

@ResponseStatus(HttpStatus.NOT_FOUND)
class HelloNotFoundException() : RuntimeException("Hello not found")

@Configuration
class SecurityConfig() : WebSecurityConfigurerAdapter() {
    override fun configure(http: HttpSecurity) {
        http.anonymous().and().authorizeRequests().anyRequest().permitAll()
    }
}

Expected Behavior

Calling GET http://localhost:8080/hello returns a response with mediatype application/problem+json and a corresponding body.

Actual Behavior

A response with mediatype application/json is returned and the body matches the default spring boot error view json.

Possible Fix

Steps to Reproduce

  1. Create a Spring Boot project with web + security + kotlin
  2. Paste the code above in the main application file
  3. Execute the following test:
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get
import org.zalando.problem.spring.common.MediaTypes

@SpringBootTest
@AutoConfigureMockMvc
class ProblemStarterWebIssueApplicationTests {
    @Autowired
    lateinit var mockMvc: MockMvc

    @Test
    fun testProblemSupport() {
        mockMvc.get("/hello") {
            accept(MediaType.APPLICATION_JSON)
        }.andExpect {
            status { isNotFound }
            header { string(HttpHeaders.CONTENT_TYPE, MediaTypes.PROBLEM_VALUE) }
        }
    }

}
  1. The test will fail

Context

Your Environment

@whiskeysierra
Copy link
Collaborator

1. Create a Spring Boot project with web + security + kotlin

Can you post a sample pom/build file with the dependencies that yo used? Or could you create a small sample project on Github to reproduce it?

@timmhirsens
Copy link
Author

Sure, i pushed the sample including the test here: https://github.com/fr1zle/problem-web-issue

@whiskeysierra
Copy link
Collaborator

I won't have time to work on this any time soon. If anyone wants to tackle this, any help would be highly appreciated.

@timmhirsens
Copy link
Author

Any hints to where to look first? Took me a while to reproduce this, but I am willing to dig deeper with a few hints.

@whiskeysierra
Copy link
Collaborator

whiskeysierra commented Oct 8, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants