swagger-maven-plugin icon indicating copy to clipboard operation
swagger-maven-plugin copied to clipboard

"Empty" swagger.json File

Open Blackbeltsaru opened this issue 9 years ago • 22 comments
trafficstars

I am using version 3.1.0 of the swagger-maven-plugin with the following configuration: <plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.0</version> <configuration> <apiSources> <apiSource> <springmvc>true</springmvc> <locations>my.api.location</locations> <schemes>http</schemes> <host>myhost</host> <basePath>/api</basePath> <info> <title>Title</title> <version>v1</version> <description>Description</description> </info> <swaggerDirectory>my/directory</swaggerDirectory> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>

When I run mvn compile the swagger.json file generated contains only the basic information, IE the info, host, basepath, and schemes. It does not contain any information about my API.

I am using io.swagger v1.5.3 The annotations is similar to this @API(value = "value", tags = {"tag"}, hidden = false)

When checking the debug logs I see the following message: [INFO] Reflections took 136 ms to scan 1 urls, producing 38 keys and 349 values

I am able to compile the example project with the correct swagger.json so it shouldn't be an issue due to my maven version.

Am I missing something or could this be a bug.

Blackbeltsaru avatar Apr 15 '16 22:04 Blackbeltsaru

Relates to this issue

Blackbeltsaru avatar Apr 19 '16 18:04 Blackbeltsaru

I am getting the same issue with -

            <artifactId>swagger-maven-plugin</artifactId>
            <version>3.1.3</version>

deepaverma79 avatar Apr 25 '16 13:04 deepaverma79

me too, probably downgrade to 3.0-M2-SNAPSHOT should help.

TomaszWaszczyk avatar May 09 '16 09:05 TomaszWaszczyk

Chiming in to say that I'm also having the exact same issue. I'm already using springfox for dynamic generation of the swagger-ui.html and such (it this works) it uses the io.swagger annotations as well.

But with both 3.1.3 and 3.0-M2-SNAPSHOT I'm getting an empty json as well. (We need a static version, which is why I'm trying to use the plugin) So far, I see nothing while in debug, no errors, nothing.

francoisINM avatar Jun 08 '16 19:06 francoisINM

Can be related to https://github.com/kongchen/swagger-maven-plugin/issues/218?

carlosjgp avatar Jun 08 '16 22:06 carlosjgp

Doubt it, all my @RequestMapping has values

francoisINM avatar Jun 09 '16 13:06 francoisINM

+1

cnlinjie avatar Jun 24 '16 15:06 cnlinjie

Are your annotated methods public?

mortias avatar Jun 24 '16 17:06 mortias

I'm having the same issue.... My maven plugin looks similar to the above. My methods is public, and look like this:

@ApiResponses(value = {
  @ApiResponse(code = 200, message = "Gets a post"),    
  @ApiResponse(code = 401, message = "Something terrible happened", response=Error.class) })
@RequestMapping(value = "/post/{id}", method=RequestMethod.GET)
public @ResponseBody Post getPost(@ApiParam(value="ID number corresponding to a specific post") @PathVariable("id")int id)

nwagg14 avatar Jul 07 '16 15:07 nwagg14

Do you have a @RequestMapping annotation at class level?

@RequestMapping("") public class A{ @RequestMapping("/hello") public Post method(){ ... } }

If you do... can you post your class?

carlosjgp avatar Jul 07 '16 16:07 carlosjgp

I didn't have that, but adding it didn't help:

@RestController
@RequestMapping(value = "")
@Api(value="/")
public class ExampleController {

All the methods inside have a similar signature as before.

nwagg14 avatar Jul 07 '16 17:07 nwagg14

@nwagg14 Could you please share a gist with your class source code ?

who avatar Jul 07 '16 21:07 who

So my problem was that I didn't have a @ApiOperation on my methods. If anyone else is having this problem, ensure you have @RequestMapping on all methods AND your class, and that you have @ApiOperation for every method, not just @ApiResponse

nwagg14 avatar Jul 08 '16 13:07 nwagg14

@nwagg14 Can you share your original code and the fixed one using gist, please?

carlosjgp avatar Jul 08 '16 14:07 carlosjgp

Adding @ApiOperation fixed my problem. I didn't need to add @RequestMapping element

javitorres avatar Aug 10 '16 07:08 javitorres

@javitorres. If you annotate a method that is not exposed as REST endpoint (@RequestMapping from Spring), that will generate "fake" swagger documentation, isn't?

carlosjgp avatar Aug 10 '16 20:08 carlosjgp

I had this problem and no matter how I altered my project (adding removing annotiations, changing swagger and swagger maven plugin versions) i could not get the file to generate with the content that it should have contained. However, when i switched to do "mvn install swagger:generate" everything worked perfectly. This is confusing because I've never had to specify "swagger:generate" on any of the previous microservices I have developed. I'm not sure what's different about my current project, but without that maven goal the file is always empty. So now, I'll always use "swagger:generate" in my maven goal. Hope this helps others.

gsugambit avatar Nov 17 '16 19:11 gsugambit

Your plugin was declared in a pluginmanagement block maybe?

mortias avatar Nov 17 '16 20:11 mortias

@gsugambit do you have a phase block to attach your plugin to your maven life cycle?

Can you share the code? As gist if possible

carlosjgp avatar Nov 17 '16 21:11 carlosjgp

I was experiencing the same in a multi module maven setup. In my case what fixed it was to add the direct dependency from the module where the plugin is defined to the module containing the resources. It didn't pick it up through the transitive dependency. Don't know if this is a bug or the expected behavior. My 2 cents, hope it helps you.

jivimberg avatar Mar 14 '17 16:03 jivimberg

Adding RequestMapping or Api annotations doesnt help. Here is my plugin configuration below:

          <groupId>com.github.kongchen</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>3.1.7</version>
            <configuration>
                <apiSources>
                    <apiSource>
                        <springmvc>false</springmvc>
                        <locations>tr.com.mkk.kfs.kfs.web</locations>
                        <basePath>/api</basePath>
                        <info>
                            <title>Kitle Fonlama Sistemi REST API</title>
                            <description>Add some description</description>
                            <version>v1</version>
                        </info>
                        <swaggerDirectory>${project.build.directory}/swagger</swaggerDirectory>
                        <attachSwaggerArtifact>true</attachSwaggerArtifact>
                    </apiSource>
                </apiSources>
            </configuration>
            <executions>
                <execution>
                    <phase>${phase.generate-documentation}</phase>
                    <!-- fx process-classes phase -->
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

On of my rest controllers:

` @Slf4j @RestController @RequestMapping("api/member/") @Api public class MemberController {

@Autowired
MemberService memberService;

/**
 * @param saveMemberInfo
 * @return
 */

@ApiOperation(
        value = "The operation saves a new member.",
        notes = "<a href=\"https://manavgat.mkk.com.tr/api/parameter/v1/listAll\" target=\"_blank\">Click to see updatable parameters</a>")
@RequestMapping(value = Constants.MEMBER_SAVE, method = RequestMethod.POST)
public ResponseEntity<Object> saveMember(@RequestBody SaveMemberInfo saveMemberInfo) {
    memberService.saveMember(saveMemberInfo);
    return ResponseEntity.status(HttpStatus.CREATED).body(ResponseBuilder.build(saveMemberInfo, HttpStatus.CREATED, Constants.SUCCESS));
}

} `

And the generated swagger.json file:

{ "swagger" : "2.0", "info" : { "description" : "Add some description", "version" : "v1", "title" : "Kitle Fonlama Sistemi REST API" }, "basePath" : "/api" }

fukit0 avatar Jan 09 '20 13:01 fukit0

@jivimberg comment solved the issue for me, huge kudos 👏

javiercoll avatar Feb 07 '23 12:02 javiercoll