belgif / rest-problem-java

Java library for RFC 9457 Problems with support for standard problem types of the Belgif REST guide (https://www.belgif.be/specification/rest/api-guide/#error-handling)
https://belgif.github.io/rest-problem-java/
Apache License 2.0
3 stars 0 forks source link

Bug: parameter name not correctly retrieved from interface #96

Closed smals-mavh closed 1 month ago

smals-mavh commented 1 month ago

Got the following response from a Spring Boot 3.3.3 application:

{
    "type": "urn:problem-type:belgif:badRequest",
    "href": "https://www.belgif.be/specification/rest/api-guide/problems/badRequest.html",
    "title": "Bad Request",
    "status": 400,
    "detail": "The input message is incorrect",
    "issues": [
        {
            "type": "urn:problem-type:belgif:input-validation:schemaViolation",
            "title": "Input value is invalid with respect to the schema",
            "detail": "must be greater than or equal to 197",
            "in": "path",
            "name": "arg0",
            "value": 160
        }
    ]
}

Parameter annotations are only present on the (generated) interface, not on the implementation (when adding annotation to the implementation, 'name' is filled in correctly). Probably something goes wrong in reading the annotations of the parent class / interface.

jpraet commented 1 month ago

This is a BeanValidation constraint violation. I don't believe the name originates from the @PathVariable("name") annotation itself. AFAIK it originates from the parameter name in the class file, via reflection. This was introduced in Java 8, but requires a compiler flag "-parameters".

https://docs.oracle.com/en/java/javase/21/docs/specs/man/javac.html#option-parameters

Or <parameters>true</parameters> in the maven-compiler-plugin configuration.

https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#parameters

If I disable

https://github.com/belgif/rest-problem-java/blob/e8f435d0ff4b11b291a12bf57507f3c85cd7b170/pom.xml#L20

I get similar failures with "arg0" in the integration tests.

jpraet commented 1 month ago

That being said, it would probably be better if the parameter name could be extracted from annotations like PathVariable, RequestParam and RequestHeader. The name of the java method param does not necessarily match the name in the annotation.

This should be achievable with a ParameterNameProvider.