swagger-maven-plugin triggers Javadoc warning: element value must be a constant expression (but it is!) in Java annotation
Asked Answered
M

1

7

Javadoc (via Maven) is giving me the following error in one my Java JAX-RS interface method signatures:

error: element value must be a constant expression

Here is my JAX-RS interface:

public interface FooResource {

  @Consumes(APPLICATION_FORM_URLENCODED_UTF_8)
  public void bar();

}

Javdoc gives the error for @Consumes. Here is the definition for APPLICATION_FORM_URLENCODED_UTF_8, which appears in MyAppConstants in the same project:

public static final String APPLICATION_FORM_URLENCODED_UTF_8 =
    APPLICATION_FORM_URLENCODED + ";" + CHARSET_PARAMETER + "=UTF-8";

And here is the definition of APPLICATION_FORM_URLENCODED, which appears in javax.ws.rs.core.MediaType:

public final static String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";

And here is the definition of CHARSET_PARAMETER, which also appears in javax.ws.rs.core.MediaType:

public static final String CHARSET_PARAMETER = "charset";

Now I ask you: what about APPLICATION_FORM_URLENCODED_UTF_8 is not constant at compile time?

The error message didn't say that I have to provide a literal. It said I had to provide a constant. So what about this is not a constant?

(I could almost swear that this worked at one time but suddenly stopped working.)

Update: Found cause, but still don't understand.

For some reason, merely including the swagger-maven-plugin in the POM will trigger this problem! My code doesn't change at all, but as soon as I add the following dependency, suddenly I get Javadoc warnings for my existing code!!!

<dependency>
  <groupId>com.github.kongchen</groupId>
  <artifactId>swagger-maven-plugin</artifactId>
  <version>3.1.5</version>
</dependency>

How can a single dependency make Javadoc work differently on a code file? What is swagger-maven-plugin doing?

Mulligan answered 29/9, 2017 at 20:44 Comment(6)
Sure the final keyword is still there? Without it, the class variable loses its constant characteristic.Trotta
Did you try the good old clean install already?Mitchell
Yep, the final keyword is still there. Yep, I did a mvn clean install.Mulligan
Crazy --- somehow swagger-maven-plugin is triggering this. But how? See updated question.Mulligan
I'm getting the same error ("value must be a constant expression") on a constant, but my project is not using the swagger-maven-plugin.Upali
Try doing a mvn dependency:tree and seeing if an old javax.ws.rs:jsr311-api is getting pulled in via some other dependency.Mulligan
M
0

My best guess is that this happens because swagger-maven-plugin transitively (via io.swagger:swagger-core:1.5.13) an old version of the JAX-RS specification in javax.ws.rs:jsr311-api:1.1.1. Note that the JAX-RS 2 artifact ID is javax.ws.rs-api, Maven doesn't realize that they are different versions of the same JAR, and pulls them both in as dependencies. I can only guess that javax.ws.rs:jsr311-api in fact does not use constants for the variables in question. In any case, when I threw out swagger-maven-plugin and pulled in io.swagger:swagger-annotations (which was all I needed in this project for documentation), the problem went away.

See https://github.com/kongchen/swagger-maven-plugin/issues/543.

Mulligan answered 4/10, 2017 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.