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

No context for unknown property and invalid enum value errors #609

Open
aaronjwhiteside opened this issue Oct 20, 2023 · 1 comment
Open
Labels
type: improvement A minor improvement to an existing feature

Comments

@aaronjwhiteside
Copy link

aaronjwhiteside commented Oct 20, 2023

Expected Behavior

  1. Missing context for errors

I expect the SerdeException (or one of it's subclasses, InvalidFormatException, etc) to contain some context about the location of an error it encounters while trying to deserialize.

Jackson provides a getPath() method in it's JsonMappingException which is the super of things like InvalidFormatException and InvalidPropertyException, this allows one access to the json path in question.

micronaut-serialization should provide similar functionality.

At the moment it's not possible to write an ExceptionHandler to handle these situation and return a custom error message with the details.

  1. Incorrect generic exception thrown

Invalid enum values

A SerdeException is thrown instead of it's subclass InvalidFormatException,

Minified stacktrace:

io.micronaut.core.convert.exceptions.ConversionErrorException: Failed to convert argument [test] for value [null] due to: Error decoding property [TestEnum enum] of type [class mktplc.error.JacksonExceptionHandlerTest$Test]: No enum constant mktplc.error.JacksonExceptionHandlerTest.TestEnum.invalid
....
Caused by: io.micronaut.serde.exceptions.SerdeException: Error decoding property [TestEnum enum] of type [class mktplc.error.JacksonExceptionHandlerTest$Test]: No enum constant mktplc.error.JacksonExceptionHandlerTest.TestEnum.invalid
....
Caused by: java.lang.IllegalArgumentException: No enum constant mktplc.error.JacksonExceptionHandlerTest.TestEnum.invalid

If InvalidFormatException was thrown we would at least have access to it's originalValue.

Unknown property (when ignore-unknown is set to false)

When an unknown property is encountered Jackson has an IgnoredPropertyException to represent this, along with appropriate context about the field/property in question.

micronaut-serialization should provide similar functionality.

Minified stacktrace:

io.micronaut.core.convert.exceptions.ConversionErrorException: Failed to convert argument [test] for value [null] due to: Unknown property [unknown] encountered during deserialization of type: Test test
...
Caused by: io.micronaut.serde.exceptions.SerdeException: Unknown property [unknown] encountered during deserialization of type: Test test

Actual Behaviour

See Expected Behavior

Steps To Reproduce

    @Controller("/test")
    class TestController {
        @Post
        @Consumes(MediaType.APPLICATION_JSON)
        fun jackson(@Body test: Test) {
            // no-op
            LOG.info { "Request Body: $test" }
        }
    }
    
    @Serdeable
    class Test {
        var string: String? = null
        var enum: TestEnum? = null
    }
    
    @Serdeable
    enum class TestEnum { A, B, C }
    
    // apologies for this not being immediately compileable 
    
    // invalid enum value
    val ignore: HttpResponse<Void> = httpClient.toBlocking()
    .exchange(
        HttpRequest.POST(embeddedServer.uri.toASCIIString() + "/test", "{\"enum\":\"invalid\"}")
            .contentType(MediaType.APPLICATION_JSON_TYPE)
    )
    
    // unknown property 
    val ignore: HttpResponse<Void> = httpClient.toBlocking()
        .exchange(
            HttpRequest.POST(embeddedServer.uri.toASCIIString() + "/test", "{\"unknown\":\"invalid\"}")
                .contentType(MediaType.APPLICATION_JSON_TYPE)
        )

Environment Information

MacOs
JDK 17
Micronaut Platform 4.1.5

Example Application

No response

Version

Micronaut Platform 4.1.5

@graemerocher graemerocher added the type: improvement A minor improvement to an existing feature label Oct 20, 2023
@graemerocher
Copy link
Contributor

Relates to #553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants