ScaCap / spring-auto-restdocs

Spring Auto REST Docs is an extension to Spring REST Docs
https://scacap.github.io/spring-auto-restdocs/
Apache License 2.0
311 stars 86 forks source link

HumanReadableConstraintResolver Failed to create an instance of #337

Closed Russell-Allen closed 4 years ago

Russell-Allen commented 5 years ago

HumanReadableConstraintResolver is throwing a java.lang.InstantiationException caused by: java.lang.NoSuchMethodException: my.domain.Entity.()

This happens in a very specific case: I have a custom constraint which has an attribute of type class. For example, @CustConstraint(ref=Entity.class) UUID id; where the Entity class does not have a no-arg constructor.

The auto-rest-doc code is, generally speaking, trying to build a fallback description for the constraint based on its attribute values and presuming that the referenced attribute type is a constraint.

Also worth noting, if I add a custom description for the non-constraint class name (ie, my.domain.Entity) in the ConstraintDescription.properties file ... the exception goes away, as the code uses that description instead of trying to instanciate the class through a no-arg constructor.

jmisur commented 5 years ago

Thanks for reporting, we'll take a look.

fbenz commented 4 years ago

The exception shouldn't be thrown but only logged and added to the description, see https://github.com/ScaCap/spring-auto-restdocs/blob/master/spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/constraints/HumanReadableConstraintResolver.java#L90 Thus, there is no crash but the issue is reported. Am I missing something?

I guess that it's annoying to have an issue reported for such a case. So the alternative would be to change the log to INFO or remove it. Also the description could be an empty string instead of the error message. What would be your expected outcome?

Russell-Allen commented 4 years ago

Correct, this does not "crash" the overall rest doc process, and while the logs are ugly, they can be safely ignored.

I think the bigger issue is that HumanReadableConstraintResolver assumes that the class in question has a no-arg constructor.

I'm guessing that this portion of the code assumed that any class passed into the HumanReadableConstraintResolver would be a Constraint class, and thus it would always have a no-arg constructor. But that's not what's happening. This code is also being called whenever a Constraint has a property of type Class, with the value of that property.

I'm not sure what the fallback should be. Maybe something with the simple class name?