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

How to set docfx.json file and connect with swagger.json on live server #5012

Closed
uthens opened this issue Aug 22, 2019 · 9 comments
Closed

Comments

@uthens
Copy link

uthens commented Aug 22, 2019

Operation System: (Windows or Linux or MacOS)
Windows
DocFX Version Used:
v2.44.0
Template used: (default or statictoc or contain custom template)
default
Steps to Reproduce:

  1. Now it's worked on local file i save "swagger.json" file copy to folder "restapi" from debug mode, But i want to know how to link with web api online ?

Expected Behavior:
It's should support swgger.jason on online web site / live server.
And also to related issue on : docascode/docfx-seed#28

If docfx only support 'swagger.json' on local file, How to auto save 'swagger.json' to local folder ?
now on my web api, i used Swashbuckle .

Annotation 2019-08-21 180200
Annotation 2019-08-21 181354
Annotation 2019-08-21 181218

@superyyrrzz
Copy link
Contributor

Maybe a script to download swagger.json before running docfx.exe?

@uthens
Copy link
Author

uthens commented Aug 22, 2019

Hi @superyyrrzz

  1. I run debug on web APIs
  2. I manual save file swagger.json and copy file to restapi folder
  3. After that i run command line docfx.exe docfx.json --serve
  4. It's worked, i see apis methods show on docfx web site.

But i don't want to copy swagger file every time for restapi folder.
When my team modify the source code on web api.

  1. When i run debug on web APIs, How i can automatic generate swagger.json file and save to rest api folder.
  2. Swashbuckle to have some feature to save / download file to local ?

Best,
UT

@superyyrrzz
Copy link
Contributor

@yishengjin1413 @partychen Do you have any idea?

@uthens
Copy link
Author

uthens commented Aug 26, 2019

team, do you have any update or any suggestion on this ? , thank you.

@partychen
Copy link
Contributor

partychen commented Aug 27, 2019

team, do you have any update or any suggestion on this ? , thank you.

Swashbuckle has the feature to generate the swagger.json.

using System.Web.Http;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Swashbuckle.Dummy;
using Swashbuckle.Swagger;
using Newtonsoft.Json;
using Swashbuckle.Application;

namespace Swashbuckle.Tests
{
    [TestFixture]
    public class Spike
    {
        [Test]
        public void GetSwagger()
        {
            var httpConfig = new HttpConfiguration();

            // 1) Apply your WebApi config.
            WebApiConfig.Register(httpConfig);
            httpConfig.EnsureInitialized();

            // 2) Generate in-memory swagger doc
            var swaggerProvider = new SwaggerGenerator(
                httpConfig.Services.GetApiExplorer(),
                httpConfig.Formatters.JsonFormatter.SerializerSettings,
                new Dictionary<string, Info> { { "v1", new Info { version = "v1", title = "My API" } } },
                new SwaggerGeneratorOptions(
                    // apply your swagger options here ...
                    schemaIdSelector: (type) => type.FriendlyId(true),
                    conflictingActionsResolver: (apiDescriptions) => apiDescriptions.First()
                )
            );
            var swaggerDoc = swaggerProvider.GetSwagger("http://tempuri.org/api", "v1");

            // 3) Serialize
            var swaggerString = JsonConvert.SerializeObject(
                swaggerDoc,
                Formatting.Indented,
                new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    Converters = new[] { new VendorExtensionsConverter() }
                }
            );
        }
    }
}

also you can download the json file from Swashbuckle host site.
image

@superyyrrzz
Copy link
Contributor

Before you run docfx.exe docfx.json --serve, I think you have already host the swagger file properly during debugging web API. Then you can simply run Invoke-WebRequest -Uri $url -OutFile $output to download it. You may need some script work to automate this.

@uthens
Copy link
Author

uthens commented Aug 27, 2019

Thanks you so much for updated on comment and i try and found some problem it's not support .net core ?, i used vs 2019

using Swashbuckle.Dummy;
using Swashbuckle.Swagger;
using Swashbuckle.Application;

URL : https://www.nuget.org/packages/Swashbuckle/

Annotation 2019-08-27 142523

@KalleOlaviNiemitalo
Copy link

it's not support .net core ?

Perhaps you should be using https://www.nuget.org/packages/Swashbuckle.AspNetCore/ instead.

@uthens
Copy link
Author

uthens commented Sep 2, 2019

I try other solution and I think resolved on this issue : https://stackoverflow.com/questions/5566942/how-to-get-a-json-string-from-url

@uthens uthens closed this as completed Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants