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

ClassNotFoundException from ApiModelProperty and dataType #12

Open
benco33 opened this issue Dec 29, 2022 · 3 comments
Open

ClassNotFoundException from ApiModelProperty and dataType #12

benco33 opened this issue Dec 29, 2022 · 3 comments

Comments

@benco33
Copy link

benco33 commented Dec 29, 2022

I'm upgrading a Scala + Play app (2.11.8 --> 2.12.8 and 2.5.12 --> 2.8.16, respectively) and was having issues upgrading to 1.7.1 of io.swagger:swagger-play2, presumably due to transient dependencies as described in https://stackoverflow.com/a/68015459, which is how I discovered this fork.

Since switching to this fork in my upgrade branch, I did initially experience numerousClassNotFoundException errors relating to the dataType field under ApiImplicitParam, which I was able to resolve by switching over to using dataTypeClass and classOf per swagger-api#174.

However, I experienced a very similar error coming from an @ApiModelProperty annotation, like:

@ApiModelProperty(dataType = "com.org.full.big.long.classpath.but.defined.in.same.file.as.this.annotation.MyClassOfInterest")

resulting in:

java.lang.ClassNotFoundException: MyClassOfInterest
at java.base / java.net.URLClassLoader.findClass (URLClassLoader.java: 476)
at java.base / java.lang.ClassLoader.loadClass (ClassLoader.java: 589)
...

I was not able to resolve this exception in a similar manner to ApiImplicitParam as it appears the dataTypeClass is not available for ApiModelProperty. Is this a remaining / known bug or likely user error? Suggestions / workarounds?

@dwickern
Copy link
Owner

You shouldn't need to use dataType if the class field/getter is already declared as MyClassOfInterest. The only situations I needed it were to specify an erased type like Option[Int] or the subtype of scala.Enumeration#Value.

Maybe there's a problem building the classpath in your use-case. Are you using this project directly or via sbt-swagger-play?

@benco33
Copy link
Author

benco33 commented Jan 3, 2023

Thanks for the reply @dwickern.

So, I'll say that I'm new to this project and tasked with this upgrade, so I don't have a lot of historical knowledge as to why things were done. I'm not necessarily trying to define dataType for this ApiModelProperty myself, but rather this choice is existing code and now resulting in the above error upon startup.

if the class field/getter is already declared as MyClassOfInterest

I'm not clear if you mean within the context of Swagger or in general. The class is defined within the same package, so it's not a typo or undeclared.

As mentioned previously I had to refactor all of the dataType fields of ApiImplicitParam to dataTypeClass using classOf to resolve similar / same ClassNotFoundException errors, but the dataTypeClass member is not available for ApiModelProperty apparently.

Also, we're using this project directly.

@dwickern
Copy link
Owner

dwickern commented Jan 3, 2023

I'm not clear if you mean within the context of Swagger or in general. The class is defined within the same package, so it's not a typo or undeclared.

What I mean is, if the declared type is the same, the dataType annotation would be redundant. Jackson will discover it via reflection. These should be equivalent:

case class MyModel(
  @ApiModelProperty(dataType = "com.org.full.big.long.classpath.but.defined.in.same.file.as.this.annotation.MyClassOfInterest")
  myProperty: MyClassOfInterest
)

case class MyModel(
  @ApiModelProperty
  myProperty: MyClassOfInterest
)

case class MyModel(
  myProperty: MyClassOfInterest
)

Does Play 2.7 work for you with io.swagger:swagger-play2:1.7.1? Then do you see the error when you swap it out for this project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants