kongchen / swagger-maven-plugin

JAX-RS & SpringMVC supported maven build plugin, helps you generate Swagger JSON and API document in build phase.
http://kongchen.github.io/swagger-maven-plugin/
Apache License 2.0
761 stars 450 forks source link

Mustache html output not generating properly #44

Closed dcw312 closed 10 years ago

dcw312 commented 10 years ago

The mustache html output is not formatting correctly. The class attribute is only included in the first resource. The div id's are are all set to "accordion0api" The "index" attribute does not seem to be set correctly.

<h3>Resources</h3>
<h4>/package</h4>
<div id="accordion0api" class="ui-accordion ui-widget ui-helper-reset" role="tablist">

<h4>/cart</h4>
<div id="accordion0api">

I am using:
 <dependency>
                <groupId>com.wordnik</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>1.3.2</version>
</dependency>
        <plugin>
            <groupId>com.github.kongchen</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <!--<version>2.0-SNAPSHOT</version>-->
            <version>2.0</version>
            <configuration>
                <apiSources>
                    <apiSource>
                        <!--<locations>com.foo.bar.apis;com.foo.bar.apis.internal.Resource</locations>-->
                        <locations>com.bestbuy.activateddevices.beagle.resources</locations>
                            <!--;com.bestbuy.activateddevices.domain.accountline</locations>-->
                        <apiVersion>v1</apiVersion>
                        <basePath>/beagle</basePath>
                        <outputTemplate>
                            https://raw.github.com/kongchen/api-doc-template/master/v2.0/html.mustache
                        </outputTemplate>
                        <!--https://raw.github.com/kongchen/api-doc-template/master/v2.0/markdown.mustache
                            https://raw.github.com/kongchen/api-doc-template/master/v2.0/html.mustache

                        -->
                        <outputPath>generated/beagle-api.html</outputPath>
                        <swaggerDirectory>generated/apidocs</swaggerDirectory>
                        <!--useOutputFlatStructure>false</useOutputFlatStructure-->
                        <!--mustacheFileRoot>${basedir}/src/main/resources/</mustacheFileRoot-->
                    </apiSource>
                </apiSources>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
dcw312 commented 10 years ago

Looking at MavenDocumentSource, I think I should mention that I'm using jersey. JaxrsApiReader reader = new DefaultJaxrsApiReader();

dcw312 commented 10 years ago

Stepping through this code and mine, I see that MavenDocumentSource#getDocFromClass always returns a position = 0. I'm assuming that I didn't do anything wrong to cause that. The position is determined by the reader, so it is not under your control. I recommend that you handle it by inspecting the indexes of the MustacheDocument and incrementing them if they are all zero in the OutputTemplate#feedSource method. I will submit a pull request and implement if you like.

dcw312 commented 10 years ago

Updated here, but have not added test case for this.

https://github.com/dcw312/swagger-maven-plugin/blob/master/src/main/java/com/github/kongchen/swagger/docgen/mustache/MustacheDocument.java

https://github.com/dcw312/swagger-maven-plugin/blob/master/src/main/java/com/github/kongchen/swagger/docgen/mustache/OutputTemplate.java

kongchen commented 10 years ago

@dcw312 Could you please send a pull request?

dcw312 commented 10 years ago

I submitted a pull request. See issue #45

kongchen commented 10 years ago

Thanks for the patch!

aorfevre commented 10 years ago

Hi everyone,

I'm expecting the same issue as mentionned. The HTML generated is correct only for the first element.

I have tested plugin version without success. Help will be much appreciated.

here is my current configuration (but also tested with 1.3.2)

<dependency>
            <groupId>com.wordnik</groupId>
            <artifactId>swagger-jaxrs_2.10</artifactId>
            <version>1.3.5</version>
        </dependency>

And the plugin definition (I have tested with different version of plugin too)

<plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>2.3-SNAPSHOT</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <locations>com.rizze.sky;</locations>
                            <apiVersion>v1</apiVersion>
                            <basePath>http://localhost:8080/api</basePath>
                            <outputTemplate>template_mustach</outputTemplate>
                            <outputPath>target/docs/strapdown.html</outputPath>
                            <swaggerDirectory>target/docs/apidocs</swaggerDirectory>
                            <!--useOutputFlatStructure>false</useOutputFlatStructure-->
                            <!--mustacheFileRoot>${basedir}/src/main/resources/</mustacheFileRoot-->
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Thank you in advance for support and sorry if I made a duplicate or something like that ...

kongchen commented 10 years ago

Hi @aorfevre , I noticed you use <outputTemplate>template_mustach</outputTemplate>, what's that template? Does it as same as https://raw.github.com/kongchen/api-doc-template/master/v2.0/html.mustache ?

aorfevre commented 10 years ago

hi kongchen,

After many tests, I have finally decided to change the template by customizing it a bit. My base was indeed html.mustache. It's now working with my specific adjustement the way I wanted too.

Thank you for support.

kongchen commented 10 years ago

Sure, glad to hear that.

eskalera commented 10 years ago

Hi @kongchen

I have the same problem as @aorfevre .

Here is my dependency

    <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
        <scope>compile</scope>
        <version>1.3.7</version>
    </dependency>

and my configuration

        <plugin>
            <groupId>com.github.kongchen</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <apiSources>

                    <apiSource>
                        <locations>org.gradiant.criptonube.rest.api</locations>
                        <apiVersion>1.0</apiVersion>
                        <basePath>http://193.146.211.61:8080/HSMaaS/webresources</basePath>
                        <swaggerDirectory>generated/swagger-ui</swaggerDirectory>
                        <outputTemplate>templates/strapdown.html.mustache</outputTemplate>
                        <mustacheFileRoot>${basedir}/templates/</mustacheFileRoot>
                        <outputPath>${basedir}/generated/document.html</outputPath>
                    </apiSource>

                </apiSources>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

swagger-ui folder with json is there but no document.html is being genrated.

kongchen commented 10 years ago

Do you sure you have prepared all of your template files? such as https://github.com/kongchen/swagger-maven-example/tree/master/templates

eskalera commented 10 years ago

Yes I have. I copied that very same folder in my project. I am not sure if I need to download the swagger dist content as well. I mean, swagger maven plugin is all I am using so far.

kongchen commented 10 years ago

Have you ever run https://github.com/kongchen/swagger-maven-example successfully in your env? If so, compare your pom.xml with the example's.

eskalera commented 10 years ago

Yes I have. I cloned the repo. Did mvn clean install and launched the war with jetty. I access http://localhost:8080/swagger-maven-example-1.0-SNAPSHOT/ but not sure how to access the document.html. It does generate it in the generated folder though.

kongchen commented 10 years ago

Hi @eskalera , this plugin is a compile phase plugin and the generated document is a static document. The swagger-maven-example is only an example to prove that, you only need to open the document.html using your native browser (ie, chrome, firefox).