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

Generate multiple-files based on different WCF classes or tags #204

Open
karimjouini opened this issue Apr 7, 2020 · 2 comments
Open

Generate multiple-files based on different WCF classes or tags #204

karimjouini opened this issue Apr 7, 2020 · 2 comments

Comments

@karimjouini
Copy link

Hi,

We are faced with the current issue: a single WCF project has multiple SVCs, and we want a separate json file for each (so we can re-use these as different services/subscriptions for our clients.

Is there a way to set SwaggerWCF to do it ? or an easy way to post-process?

Thanks!

@kochobay
Copy link

Dear karimjouini, did you manage to find a solution for this? We have faced the same issue recently.

@kochobay
Copy link

kochobay commented Jul 21, 2020

We found a workaround for it.
We used SwaggerWcfTagAttribute to give same tag for same service.
While [SwaggerWcfTag("Mercury")] is used for all operations in ServiceA.svc, [SwaggerWcfTag("Venus")] is used for the ones in ServiceB.svc, etc.

Then we created routes for each tag in Global.asax.
Finally, based on requested url, we have set hidden ones.
I mean that if request is like http://server.com/Mercury-docs, then we set Venus & Earth as hidden.

Here is the code we used in Global.asax.

public class Global : System.Web.HttpApplication
{
private static string[] SwaggerTags = new string[] { "Mercury", "Venus", "Earth" };

protected void Application_Start(object sender, EventArgs e)
{
	SwaggerWcfEndpoint.FilterVisibleTags = FilterVisibleTags;
	SwaggerWcfEndpoint.FilterHiddenTags = FilterHiddenTags;
	SwaggerWcfEndpoint.DisableSwaggerUI = false;

	RouteTable.Routes.Add(new ServiceRoute("api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
	foreach(string tag in SwaggerTags)
		RouteTable.Routes.Add(new ServiceRoute($"{tag}-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
}

private static List<string> FilterVisibleTags(string path, List<string> visibleTags)
{
	if (null != HttpContext.Current)
	{
		string urlPart = HttpContext.Current.Request.Url.AbsolutePath.ToLower();
		foreach (string tag in SwaggerTags)
		{
			if (urlPart.Contains($"{tag.ToLower()}-docs"))
				visibleTags.Add(tag);
		}
	}

	return visibleTags;
}

private static List<string> FilterHiddenTags(string path, List<string> hiddenTags)
{
	if (null != HttpContext.Current)
	{
		string urlPart = HttpContext.Current.Request.Url.AbsolutePath.ToLower();
		foreach (string tag in SwaggerTags)
		{
			if (urlPart.Contains($"{tag.ToLower()}-docs"))
				hiddenTags.AddRange(SwaggerTags.Where(t => !t.Equals(tag)));
		}
	}

	return hiddenTags;
}

}

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

2 participants