Skip to content

MissingKotlinParameterException should extend MismatchedInputException #321

@MateuszStefek

Description

@MateuszStefek

Should MissingKotlinParameterException extend MismatchedInputException?

The javadoc of MismatchedInputException suggests that MissingKotlinParameterException is a valid subtype:

/**
 * General exception type used as the base class for all {@link JsonMappingException}s
 * that are due to input not mapping to target definition; these are typically
 * considered "client errors" since target type definition itself is not the root cause
 * but mismatching input. This is in contrast to {@link InvalidDefinitionException} which
 * signals a problem with target type definition and not input.
 *<p>

In a recent version of Spring, a change was introduced that differentiate handling of exceptions when parsing a HTTP input:
https://github.com/spring-projects/spring-framework/blob/b889700548681382911e06701b257549bb81e929/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java#L242

		catch (MismatchedInputException ex) {  // specific kind of JsonMappingException
			throw new HttpMessageNotReadableException("Invalid JSON input: " + ex.getOriginalMessage(), ex, inputMessage);
		}
		catch (InvalidDefinitionException ex) {  // another kind of JsonMappingException
			throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex);
		}
		catch (JsonMappingException ex) {  // typically ValueInstantiationException
			throw new HttpMessageConversionException("JSON conversion problem: " + ex.getOriginalMessage(), ex);
		}
		catch (JsonProcessingException ex) {
			throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex, inputMessage);
		}

Basically, if Jackson throws MismatchedInputException our controllers respond with 400 Bad Request, otherwise they respond with 500 Internal Server Error.

Because of this change, Kotlin dtos cannot be properly rejected when a non-nullable constructor argument is missing.

An example problem it causes:
spring-projects/spring-framework#24610

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions