GeumLiAh
GeumLiAh

Reputation: 11

Springfox error "Unable to find a model that matches key" for {namespace='java.lang', name='Class�ConstraintValidator�object,object��'}

I'm getting these error messages in logs:

[12:15 11:48:20.957] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�ConstraintValidator�object,object��'}, viewDiscriminator=com.test.entity.view.View$External, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.957] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�object�'}, viewDiscriminator=com.test.entity.view.View$External, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.958] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�Payload�'}, viewDiscriminator=com.test.entity.view.View$External, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.991] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�ConstraintValidator�object,object��'}, viewDiscriminator=null, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.991] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�object�'}, viewDiscriminator=null, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.992] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�Payload�'}, viewDiscriminator=null, validationGroupDiscriminators=[], isResponse=true}

It seems as custom validation annotations are somehow involved and related to them fields are treated as models or smth. Example:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = CardDocumentValidator.class)
@Documented
public @interface CardDocument {
    String message();

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

public class CardDocumentValidator implements ConstraintValidator<CardDocument, TestEntity> {...}

I'm using Spring Boot 2.5.5 with springfox-boot-starter 3.0.0. However I'm still on OpenApi 2.0, so I have configured the property springfox:documentation:swagger:use-model-v3: false, which helped me to get rid of similar errors.

In api-docs json among definitions:

        "ConstraintDescriptor«object»": {
            "type": "object",
            "properties": {
                "annotation": {
                    "type": "object"
                },
                "attributes": {
                    "type": "object"
                },
                "composingConstraints": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/ConstraintDescriptor«object»"
                    }
                },
                "constraintValidatorClasses": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«ConstraintValidator«object,object»»'}"
                    }
                },
                "groups": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«object»'}"
                    }
                },
                "messageTemplate": {
                    "type": "string"
                },
                "payload": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«Payload»'}"
                    }
                },
                "reportAsSingleViolation": {
                    "type": "boolean"
                },
                "validationAppliesTo": {
                    "type": "string",
                    "enum": [
                        "IMPLICIT",
                        "PARAMETERS",
                        "RETURN_VALUE"
                    ]
                },
                "valueUnwrapping": {
                    "type": "string",
                    "enum": [
                        "DEFAULT",
                        "SKIP",
                        "UNWRAP"
                    ]
                }
            },
            "title": "ConstraintDescriptor«object»"
        },
        "ConstraintDescriptor«object»DefaultView": {
            "type": "object",
            "properties": {
                "annotation": {
                    "type": "object"
                },
                "attributes": {
                    "type": "object"
                },
                "composingConstraints": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/ConstraintDescriptor«object»DefaultView"
                    }
                },
                "constraintValidatorClasses": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«ConstraintValidator«object,object»»'}"
                    }
                },
                "groups": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«object»'}"
                    }
                },
                "messageTemplate": {
                    "type": "string"
                },
                "payload": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«Payload»'}"
                    }
                },
                "reportAsSingleViolation": {
                    "type": "boolean"
                },
                "validationAppliesTo": {
                    "type": "string",
                    "enum": [
                        "IMPLICIT",
                        "PARAMETERS",
                        "RETURN_VALUE"
                    ]
                },
                "valueUnwrapping": {
                    "type": "string",
                    "enum": [
                        "DEFAULT",
                        "SKIP",
                        "UNWRAP"
                    ]
                }
            },
            "title": "ConstraintDescriptor«object»DefaultView"
        },
        "ConstraintDescriptor«object»ExternalView": {
            "type": "object",
            "properties": {
                "annotation": {
                    "type": "object"
                },
                "attributes": {
                    "type": "object"
                },
                "composingConstraints": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/ConstraintDescriptor«object»ExternalView"
                    }
                },
                "constraintValidatorClasses": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«ConstraintValidator«object,object»»'}"
                    }
                },
                "groups": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«object»'}"
                    }
                },
                "messageTemplate": {
                    "type": "string"
                },
                "payload": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«Payload»'}"
                    }
                },
                "reportAsSingleViolation": {
                    "type": "boolean"
                },
                "validationAppliesTo": {
                    "type": "string",
                    "enum": [
                        "IMPLICIT",
                        "PARAMETERS",
                        "RETURN_VALUE"
                    ]
                },
                "valueUnwrapping": {
                    "type": "string",
                    "enum": [
                        "DEFAULT",
                        "SKIP",
                        "UNWRAP"
                    ]
                }
            },
            "title": "ConstraintDescriptor«object»ExternalView"
        },
        "ConstraintValidator«object,object»": {
            "type": "object",
            "title": "ConstraintValidator«object,object»"
        },
        "ConstraintValidator«object,object»DefaultView": {
            "type": "object",
            "title": "ConstraintValidator«object,object»DefaultView"
        },
        "ConstraintValidator«object,object»ExternalView": {
            "type": "object",
            "title": "ConstraintValidator«object,object»ExternalView"
        },
        "ConstraintViolation": {
            "type": "object",
            "properties": {
                "constraintDescriptor": {
                    "$ref": "#/definitions/ConstraintDescriptor«object»"
                },
                "executableParameters": {
                    "type": "array",
                    "items": {
                        "type": "object"
                    }
                },
                "executableReturnValue": {
                    "type": "object"
                },
                "invalidValue": {
                    "type": "object"
                },
                "leafBean": {
                    "type": "object"
                },
                "message": {
                    "type": "string"
                },
                "messageTemplate": {
                    "type": "string"
                },
                "propertyPath": {
                    "$ref": "#/definitions/Path"
                },
                "rootBean": {
                    "type": "object"
                }
            },
            "title": "ConstraintViolation"
        },
        "ConstraintViolationDefaultView": {
            "type": "object",
            "properties": {
                "constraintDescriptor": {
                    "$ref": "#/definitions/ConstraintDescriptor«object»DefaultView"
                },
                "executableParameters": {
                    "type": "array",
                    "items": {
                        "type": "object"
                    }
                },
                "executableReturnValue": {
                    "type": "object"
                },
                "invalidValue": {
                    "type": "object"
                },
                "leafBean": {
                    "type": "object"
                },
                "message": {
                    "type": "string"
                },
                "messageTemplate": {
                    "type": "string"
                },
                "propertyPath": {
                    "$ref": "#/definitions/PathDefaultView"
                },
                "rootBean": {
                    "type": "object"
                }
            },
            "title": "ConstraintViolationDefaultView"
        },
        "ConstraintViolationExternalView": {
            "type": "object",
            "properties": {
                "constraintDescriptor": {
                    "$ref": "#/definitions/ConstraintDescriptor«object»ExternalView"
                },
                "executableParameters": {
                    "type": "array",
                    "items": {
                        "type": "object"
                    }
                },
                "executableReturnValue": {
                    "type": "object"
                },
                "invalidValue": {
                    "type": "object"
                },
                "leafBean": {
                    "type": "object"
                },
                "message": {
                    "type": "string"
                },
                "messageTemplate": {
                    "type": "string"
                },
                "propertyPath": {
                    "$ref": "#/definitions/PathExternalView"
                },
                "rootBean": {
                    "type": "object"
                }
            },
            "title": "ConstraintViolationExternalView"
        },

Upvotes: 0

Views: 1498

Answers (1)

GeumLiAh
GeumLiAh

Reputation: 11

Found the culprit :) private Set validationErrors in one of the base entity classes. So ConstraintViolation was added to Models and ConstraintDescriptor and its fields' classes which included:

private final List<Class<? extends ConstraintValidator<T, ?>>> constraintValidatorClasses;

private final Set<Class<?>> groups;

private final Set<Class<? extends Payload>> payloads;

Putting @JsonIgnore helped. Another way is to add @ApiModelProperty(hidden = true) which doesn't affect serialization by simply hiding the field from being documented.

Upvotes: 0

Related Questions