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

feat(SwaggerUI): Add Swagger UI configuration parameters #6314

Closed

Conversation

aoudiamoncef
Copy link
Contributor

Hi @dmlloyd ,
This PR is related to #5639. I'm waiting for feedbacks
Moncef

Copy link
Member

@gsmet gsmet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I added a couple of suggestions and questions.

* The default expansion depth for the model on the model-example section.
*/
@ConfigItem
OptionalInt defaultModelExpandDepth;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a bit confusing to have an option with an s and one without. It's like that in Swagger UI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too, i just copied the original configuration properties names.

@gsmet
Copy link
Member

gsmet commented Jan 3, 2020

My opinion on this one is that adding things to the URL won't work very well.

I would lean towards trying to find another solution.

@Postremus
Copy link
Member

@aoudiamoncef Do you need any help on this pr? I kinda need this feature.

@aoudiamoncef
Copy link
Contributor Author

I'm agree with @gsmet, we should be able to inject swagger ui configuration at build time.
We are using the pre-packaged webjar of swagger, we couldn't inject a swagger.yml configuration file which could be the best solution.

@Postremus
Copy link
Member

I think we are still able to do this, or at least something similar.
The unpackaged swagger-ui is inside "META-INF/swagger-ui-files". Would it be enough to put a generated swagger.yml in this directory?

Alternativly, we could also "just" generate a custom index.html, and configure the swagger-ui this way.

@aoudiamoncef
Copy link
Contributor Author

@Postremus, we should look at how they inject the config file.

@Postremus
Copy link
Member

Ah, I undertstand. The swagger.yml config is staticly included into "swagger-ui.js" during the compilation of the ui.

Another option:
We could generate an index.html, where we configure swagger-ui to load the configuration from a custom configUrl.
We could then just generate a swagger.yml config file at build time, and serve it with quarkus.
The configUrl would point to this static config file.

WDYT?

@aoudiamoncef
Copy link
Contributor Author

I think that keeping the config properties and inject them inside the html file could be the easiest way to do it.

@gsmet
Copy link
Member

gsmet commented Apr 22, 2020

@aoudiamoncef any chance you could revisit this by pushing the configuration directly by adjusting the index.html?

It would be similar to what we do here: https://github.com/quarkusio/quarkus/blob/master/extensions/swagger-ui/deployment/src/main/java/io/quarkus/swaggerui/deployment/SwaggerUiProcessor.java#L189 . Except we would push all the configuration properties there.

IMHO, this would be far more future proof.

@aoudiamoncef
Copy link
Contributor Author

@gsmet, I'll do it ASAP.

@gsmet
Copy link
Member

gsmet commented Apr 22, 2020

Just a note that you will need to be a bit careful about escaping :).

@gsmet gsmet added this to the 1.5.0 milestone Apr 22, 2020
@aoudiamoncef
Copy link
Contributor Author

I'm working on pushing configuration to index.html, my idea is to write a Java RegEx to get:

 const ui = SwaggerUIBundle({ // start with this line
        url: "https://petstore.swagger.io/v2/swagger.json",
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout"
      }) // and ends with this one

Then i'll write a kind of string template and inject the configuration Map. Anyone have a better approach ?

@gsmet
Copy link
Member

gsmet commented Apr 22, 2020

Yeah, I don't think it would be too hard. Just catch SwaggerUIBundle\\(({[^}]+)} with a multiline regexp and it would be safe enough.

With this approach, we could also fix #4766 (see my last comment).

@gsmet
Copy link
Member

gsmet commented Apr 22, 2020

Additional though: jackson-databind is a dependency of smallrye-openapi. Considering there's a good chance Swagger UI will always be used with openapi, I think you could add the quarkus-jackson extension to handle the JSON generation in the Swagger UI extension.

@aoudiamoncef
Copy link
Contributor Author

I'm using this RegEx (SwaggerUIBundle\()(.*)(\)) with Dotall and i got:
image
The idea is to be able to generate a Javascript object using jackson:

ObjectMapper mapper = new ObjectMapper()
        .disable(JsonGenerator.Feature.QUOTE_FIELD_NAMES)
        .enable(SerializationFeature.INDENT_OUTPUT);

We have to replace the group(2) .

@gsmet
Copy link
Member

gsmet commented Apr 28, 2020

@aoudiamoncef are you making progress? Do you need any help?

@aoudiamoncef
Copy link
Contributor Author

aoudiamoncef commented Apr 28, 2020

@gsmet

I'm still working on it but with another approach. It's not a big deal to create a string and replace the matched group.

I'm trying to validate the configurations at build time.

Handling authentication needs more work and we should create tests to avoid regressions in the future.

@aoudiamoncef
Copy link
Contributor Author

aoudiamoncef commented May 6, 2020

Hi @gsmet,
Updates:
Inject available Swagger UI settings: done
Inject Oauth2 settings: done
Integration test: done (it works as expected on openapi swagger quickstart)
Add Swagger group handling: work in progress(took me more time than i expected).

I'll push my work ASAP

@gsmet
Copy link
Member

gsmet commented May 6, 2020

Cool! Thanks for the heads up.

@aoudiamoncef
Copy link
Contributor Author

Hi @gsmet,
I'm waiting for feedbacks to improve the code(it works but...) and try to pretty print all the generated configuration.

aoudiamoncef added a commit to aoudiamoncef/quarkus that referenced this pull request May 11, 2020
aoudiamoncef added a commit to aoudiamoncef/quarkus that referenced this pull request May 17, 2020
Copy link
Member

@gsmet gsmet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, very nice and thorough work, thanks a lot for that!

Could you have a look at my comments?

(sorry for the late review)

extensions/swagger-ui/deployment/pom.xml Outdated Show resolved Hide resolved
.disable(JsonWriteFeature.QUOTE_FIELD_NAMES)
.enable(SerializationFeature.INDENT_OUTPUT)
.addModule(new Jdk8Module())
.build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we initialize this one statically?

quarkus.swagger-ui.urls=/openapi?group=test, /openapi?group=lahzouz
quarkus.swagger-ui.validator-url=https://validator.swagger.io/validator

quarkus.swagger-ui.oauth=true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here?

@gsmet gsmet removed this from the 1.5.0.CR1 milestone May 19, 2020
aoudiamoncef added a commit to aoudiamoncef/quarkus that referenced this pull request May 21, 2020
@aoudiamoncef
Copy link
Contributor Author

@gsmet, i did the requested changes. I'm waiting for your feedbacks.

aoudiamoncef added a commit to aoudiamoncef/quarkus that referenced this pull request May 26, 2020
aoudiamoncef added a commit to aoudiamoncef/quarkus that referenced this pull request Jun 1, 2020
@gsmet gsmet modified the milestones: 1.6.0 - master, 1.7.0 Jun 25, 2020
aoudiamoncef added a commit to aoudiamoncef/quarkus that referenced this pull request Jun 26, 2020
@gsmet
Copy link
Member

gsmet commented Jul 6, 2020

Hi Moncef,

Sorry it took me so long to get back to you.

The PR seems broken, I had to change this in the test:

public class CustomConfigTest {

    @RegisterExtension
    static final QuarkusUnitTest config = new QuarkusUnitTest()
            .withConfigurationResource("application-custom-config.properties");

but then the tests are failing anyway.

I think it won't fly to generate the URIs in the config as you somehow need to use httpRootPathBuildItem.adjustPath() on the paths as we do it in the processor. I would probably move all the JSON object building logic to a proper method in the processor.

Then there are other minor things that could be improved but I think we need that to be addressed first.

@aoudiamoncef
Copy link
Contributor Author

aoudiamoncef commented Jul 6, 2020

Hi @gsmet,
I'll update the PR ASAP, tests passed few months ago, but i should write better tests.
I remember @phillip-kruger proposal to fuse SwaggeUI extension with OpenApi one. It could make sense with GROUPS, OpenApi extension have to know about this, in order to create supplied GROUPS.

Thanks

@phillip-kruger
Copy link
Member

When I suggested that we merge the two, there was concerns that maybe we want to add support for other UIs. I still think we can merge and add support for other UIs via config, as there will not be many (if ever more than one or two). This will make things easier to maintain I think.

@gsmet gsmet removed this from the 1.7.0 - master milestone Jul 29, 2020
aoudiamoncef added a commit to aoudiamoncef/quarkus that referenced this pull request Aug 7, 2020
@wtrocki
Copy link

wtrocki commented Oct 6, 2020

Maybe other editors could be a separate issue once this one is merged?
Things like Apicurio/Apicurito will be an amazing addition as they can support other schemas.

@matus-m
Copy link

matus-m commented Oct 14, 2020

Hi guys, what is the state of this MR? We would love to get support for swagger-ui showing multiple openapi files (dropdown in header) for which we need to be able to specify urls attribute of the Swagger ui config.

{url: "<url1>", name: "<name1>"},{url: "<url2>", name: "<name2>"}

I understand that this MR would allow that among other things.
thanks[[m56

@phillip-kruger
Copy link
Member

Hi all.
I am looking to get this into smallrye. So far the plan is to allow creating a index html using a basic template and some options. w.d.y.t ?

@matus-m
Copy link

matus-m commented Oct 16, 2020

any solution that would allow customization of the swagger config options is fine. Right now there is no way how to customize it(I tried adding a webfilter that would modify the html response, but the swagger-ui is server via vertx...). Not sure how long the integration w smallrye would take. We will probably create a custom extension (based on this MR)in the meantime as we need it rather urgently. Thanks for the effort, will keep an eye on this.

@aoudiamoncef
Copy link
Contributor Author

Hi all,
I'll try to finish this PR ASAP.
I'm sorry but with my current daily work, I haven't a lot of free time to work on open source projects.

@phillip-kruger
Copy link
Member

@aoudiamoncef i have added this to SmallRye and will pull that into Quarkus soon.

@phillip-kruger
Copy link
Member

#12782 will supersede and close this.

@gsmet gsmet closed this in #12782 Oct 20, 2020
@gsmet gsmet added the triage/invalid This doesn't seem right label Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/swagger-ui triage/invalid This doesn't seem right
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants