swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Deep linking for models

Open ghost opened this issue 9 years ago • 16 comments

For large complex models, it'd be very nice to be able to click on a type that's referenced in some property and have it link to the schema for that type (especially since types aren't displayed in any particular order, so scrolling to find them can be tedious). The current styling actually makes the type names look like links, but they're not.

ghost avatar Jun 12 '15 21:06 ghost

+1

We need the ability to display JSON models separate from APIs. The use case cares not for paths or HTTP Verbs; simply how the JSON model is structured along with its data types.

dijikul avatar Jan 25 '17 14:01 dijikul

I believe this is covered by 3.0 now. If not, please reopen.

webron avatar Mar 20 '17 21:03 webron

Can anyone give an example deeplink to a model from http://petstore.swagger.io/? They don't see to be linkable, as far as I can tell :) Thanks!

patcon avatar Sep 26 '17 03:09 patcon

Seems models aren't included? https://github.com/swagger-api/swagger-ui/issues/2884#issuecomment-315492951

Can we re-open?

patcon avatar Sep 26 '17 03:09 patcon

I too would like to see deep links for models. It would be helpful when attempting to refer a customer/partner to a particular section of the documentation.

CameronGo avatar Sep 28 '18 16:09 CameronGo

+1. I was thinking something is wrong with the way I enable Deep Linking in the setup, but it turned out there's no deep linking for model / definitions. Nice feature to have.

Lakmus85 avatar Oct 11 '18 17:10 Lakmus85

+1. It would be very nice to be able to link to these model definitions. My specific use-case is to link request and response models in sequence diagrams to swagger specs.

MrFix93 avatar Feb 22 '19 11:02 MrFix93

+1 :)

gigaga avatar Oct 11 '19 09:10 gigaga

I have the same need : link my documentation to the swagger specs

Xophe avatar Nov 06 '19 15:11 Xophe

Weird that this is still not implemented. At least I was not able to find it. Anyways, here's how you can do it:

document.addEventListener("DOMContentLoaded", function (event) {
    var elemId = window.location.hash.replace('!/', '').replace('/', '').replace('#','');
    setTimeout(function () {
        var elem = document.getElementById(elemId );
        if (elem) {
            var top = elem.offsetTop;
            window.scrollTo({ top: top, behavior: 'auto' });
            var anchor = elem.querySelector('.model');
            if (anchor) {
                anchor.click();
            }
        }
    }, 700);
});

This obviously goes into a javascript that you inject via:

c.InjectJavascript("/path/to/your-file.js");

Also remember to enable StaticFiles middleware in dot net core.

PeterKottas avatar Mar 03 '20 15:03 PeterKottas

+1: If someone would like to implement HATEOAS link relations, they would need to provide a uri to the specification of the object. I would like to use the Model object in Swagger UI for this purpose, with a deep link.

mdhtr avatar May 07 '20 18:05 mdhtr

I started implementing the part where it would be possible to expand and scroll to a model by specifying a reference to it in the URL.

I'm curious if anyone would still like to have the original feature request implemented? It was, in my understanding, to be able to navigate to a model definition by clicking that model's name in another model or in an operation definition. If yes, how would you expect it to work? Because currently if you click on a model's name it expands/collapses, so I don't know where would the link navigation fit into this behavior. Any ideas?

mdhtr avatar May 10 '20 20:05 mdhtr

@mdhtr Would it be possible to expand only from the > symbol, and have the name as a link?

olli-miettinen-arm avatar Nov 10 '20 10:11 olli-miettinen-arm

A non-breaking way for the existing expand/collapse functionality could be to add a link next to the name, with the text "link" / "permalink" / "🔗" or similar.

reinseth avatar Jan 08 '21 11:01 reinseth

@PeterKottas thanks for the code! A minor bug, if the model is near the bottom of the page it doesn't scroll down far enough, because the link hasn't been expanded yet and the offsetTop won't be correct after the click. This can be fixed by doing the scrolling after the click:

document.addEventListener("DOMContentLoaded", function (event) {
    var elemId = window.location.hash.replace('!/', '').replace('/', '').replace('#', '');
    setTimeout(function () {
        var elem = document.getElementById(elemId);
        if (elem) {
            var anchor = elem.querySelector('.model');
            if (anchor) {
                anchor.click();
            }
            setTimeout(function () {
                var top = elem.offsetTop;
                window.scrollTo({ top: top, behavior: 'auto' });
            }, 100);
        }
    }, 700);
});

rocklan avatar Jul 23 '21 02:07 rocklan

There's another approach with works with standard HTML mechanisms and does not require extra JavaScript or a patch. It is dependent on the current swagger implementation though and might change.

Every model div and an ID with the pattern model-<SchemaName>. You can directly link to it by adding the ID to the fragment of your URL.

Taking the Petstore example, you can directly jump to Category via the URL https://petstore.swagger.io#model-Category . If you want to add this link within a description of your YAML, you can use <a href="#model-Category">schema</a>

dirkbolte avatar May 10 '24 08:05 dirkbolte