Skip to content

FieldTypesDoNotMatchException when an optional field with a non-null type has a null value #365

@marceloverdijk

Description

@marceloverdijk

null in responses like

"date_of_birth": "1990-03-02",
"date_of_death": null,

are documented in the generated snippets as:

|`date_of_birth`
|`String`
|The date of birth of the driver.

|`date_of_death`
|`Null`
|The date of death of the driver.

When documenting them explicitly like:

                fieldWithPath("date_of_birth")
                        .type(STRING)
                        .description("The date of birth of the driver."),
                fieldWithPath("date_of_death")
                        .type(STRING)
                        .optional()
                        .description("The date of death of the driver."),

gives the error:

org.springframework.restdocs.payload.FieldTypesDoNotMatchException: The documented type of the field 'date_of_death' is String but the actual type is Null

As discussed on Gitter:

Andy Wilkinson @wilkinsona 20:31
I guess it would be reasonable to accept null when a field’s marked as optional

Marcel Overdijk @marceloverdijk 20:31
yes I think so

Andy Wilkinson @wilkinsona 20:31
So in your case a string or null or no field at all would be acceptable as you’ve said the field’s optional
Can you open an issue please? I’d like to do a 1.1.x release soon and I think this could be fixed there

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions