All Projects → abelsilva → Swaggerwcf

abelsilva / Swaggerwcf

Licence: apache-2.0
Swagger for WCF

Programming Languages

csharp
926 projects

Labels

Projects that are alternatives of or similar to Swaggerwcf

Koa Oai Router
Koa Router, based on OpenAPI, Swagger and Json Schema.
Stars: ✭ 97 (-19.83%)
Mutual labels:  swagger
Sbt Swagger Codegen
sbt Plugin for Scala code generation for Swagger specs
Stars: ✭ 104 (-14.05%)
Mutual labels:  swagger
Flagr
Flagr is a feature flagging, A/B testing and dynamic configuration microservice
Stars: ✭ 1,776 (+1367.77%)
Mutual labels:  swagger
Apifuzzer
Fuzz test your application using your OpenAPI or Swagger API definition without coding
Stars: ✭ 101 (-16.53%)
Mutual labels:  swagger
Swagger Test
Specification-driven REST API testing
Stars: ✭ 102 (-15.7%)
Mutual labels:  swagger
Webapiclient
An open source project based on the HttpClient. You only need to define the c# interface and modify the related features to invoke the client library of the remote http interface asynchronously.
Stars: ✭ 1,618 (+1237.19%)
Mutual labels:  swagger
Appy Backend
A user system to bootstrap your app.
Stars: ✭ 96 (-20.66%)
Mutual labels:  swagger
Netcoreblockly
.NET Core API to Blockly - generate from WebAPI, Swagger, OData, GraphQL =>
Stars: ✭ 121 (+0%)
Mutual labels:  swagger
Hapi Starter Kit
Hapi.js based REST boilerplate which uses latest ES7/ES8 features (async/await) with code coverage and follows best pratices
Stars: ✭ 103 (-14.88%)
Mutual labels:  swagger
Vscode Apielements
VSCode extensions for API Elements (aka: API Blueprint and Swagger)
Stars: ✭ 117 (-3.31%)
Mutual labels:  swagger
Swagger Github Pages
How to host Swagger API documentation with GitHub Pages
Stars: ✭ 102 (-15.7%)
Mutual labels:  swagger
Swagger Combine
Combines multiple Swagger schemas into one dereferenced schema.
Stars: ✭ 102 (-15.7%)
Mutual labels:  swagger
Pretty Swag
Pretty UI for Swagger spec
Stars: ✭ 112 (-7.44%)
Mutual labels:  swagger
Springboot Templates
springboot和dubbo、netty的集成,redis mongodb的nosql模板, kafka rocketmq rabbit的MQ模板, solr solrcloud elasticsearch查询引擎
Stars: ✭ 100 (-17.36%)
Mutual labels:  swagger
L5 Swagger
OpenApi or Swagger integration to Laravel
Stars: ✭ 1,781 (+1371.9%)
Mutual labels:  swagger
Kaizen Openapi Editor
Eclipse Editor for the Swagger-OpenAPI Description Language
Stars: ✭ 97 (-19.83%)
Mutual labels:  swagger
Falsy
FAL.S.Y python api framework(the framework for using falcon, swagger, yaml together)
Stars: ✭ 109 (-9.92%)
Mutual labels:  swagger
Nest Permissions Seed
A simple application demonstrating the basic usage of permissions with NestJS.
Stars: ✭ 121 (+0%)
Mutual labels:  swagger
Openapi Core
OpenAPI core
Stars: ✭ 119 (-1.65%)
Mutual labels:  swagger
Friboo
Utility library for writing microservices in Clojure, with support for Swagger and OAuth
Stars: ✭ 116 (-4.13%)
Mutual labels:  swagger

SwaggerWcf  nuget status

Generates Swagger (2.0) for WCF services and also provides swagger-ui.

With an API described in Swagger you can use multiple Swagger tools like client generators, see swagger-codegen for more details.

This project has started as a fork from superstator/Swaggeratr to implement version 2.0 of Swagger.

Getting Started

Step 1: Install SwaggerWcf package


Install-Package SwaggerWcf

Step 2: Configure WCF

ASP.NET

Add the route in the Application_Start method inside Global.asax

protected void Application_Start(object sender, EventArgs e)
{
    // [.......]
    
    RouteTable.Routes.Add(new ServiceRoute("api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
}

Note: You might need to add a reference to System.ServiceModel.Activation

Edit Web.config and add the following (if it doesn't exist yet) inside the system.serviceModel block

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

Edit again Web.config and add the following (if it doesn't exist yet) inside the system.webServer block

<modules runAllManagedModulesForAllRequests="true"/>

Self Hosted

Add an endpoint to your App.config file.

<services>
  <service name="SwaggerWcf.SwaggerWcfEndpoint">
    <endpoint address="http://localhost/docs" binding="webHttpBinding" contract="SwaggerWcf.ISwaggerWcfEndpoint" />
  </service>
</services>

Create a WebServiceHost

var swaggerHost = new WebServiceHost(typeof(SwaggerWcfEndpoint));
swaggerHost.Open();

Step 3: Optionaly configure WCF response auto types

Add the following to your config file. This will allow the WCF service to accept requests and send replies based on the Content-Type headers.

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior>
        <webHttp automaticFormatSelectionEnabled="true" />
      </behavior>
    </endpointBehaviors>
    <!-- [.......] -->
  </behaviors>
</system.serviceModel>
  

Step 4: Configure WCF services general information

Configure via config file

Add the following to your config file and change the values accordingly

<configSections>
  <section name="swaggerwcf" type="SwaggerWcf.Configuration.SwaggerWcfSection, SwaggerWcf" />
</configSections>

<swaggerwcf>
  <tags>
    <tag name="LowPerformance" visible="false" />
  </tags>
  <settings>
    <setting name="InfoDescription" value="Sample Service to test SwaggerWCF" />
    <setting name="InfoVersion" value="0.0.1" />
    <setting name="InfoTermsOfService" value="Terms of Service" />
    <setting name="InfoTitle" value="SampleService" />
    <setting name="InfoContactName" value="Abel Silva" />
    <setting name="InfoContactUrl" value="http://github.com/abelsilva" />
    <setting name="InfoContactEmail" value="[email protected]" />
    <setting name="InfoLicenseUrl" value="https://github.com/abelsilva/SwaggerWCF/blob/master/LICENSE" />
    <setting name="InfoLicenseName" value="Apache License" />
  </settings>
</swaggerwcf>

Notes:

  • make sure the configSections block is the first child of configuration
  • tags will be described further down

Configure via code

Configure the base properties via code. New: You can add security settings to your api (see also the new Security-Methodattribute)

var info = new Info
{
Description = "Sample Service to test SwaggerWCF",
Version = "0.0.1"
// etc
};

var security = new SecurityDefinitions
{
  {
    "api-gateway", new SecurityAuthorization
    {
      Type = "oauth2",
      Name = "api-gateway",
      Description = "Forces authentication with credentials via an api gateway",
      Flow = "password",
      Scopes = new Dictionary<string, string="">
      {
          { "author", "use author scope"},
          { "admin", "use admin scope"},
      },
      AuthorizationUrl = "http://yourapi.net/oauth/token"
    }
  }
};

SwaggerWcfEndpoint.Configure(info, security);

Step 5: Decorate WCF services interfaces

For each method, configure the WebInvoke or WebGet attribute, and add a SwaggerWcfPath attribute.

[ServiceContract]
public interface IStore
{
    [SwaggerWcfPath("Create book", "Create a book on the store")]
    [WebInvoke(UriTemplate = "/books",
        BodyStyle = WebMessageBodyStyle.Bare,
        Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    Book CreateBook(Book value);
    
    // [.......]
}

Step 6: Decorate WCF services class

Add the SwaggerWcf and AspNetCompatibilityRequirements attributes to the class providing the base path for the service (the same as used in step 2). Optinally, for each method, add the SwaggerWcfTag to categorize the method and the SwaggerWcfResponse for each possible response from the service.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[SwaggerWcf("/v1/rest")]
public class BookStore : IStore
{
    [SwaggerWcfTag("Books")]
    [SwaggerWcfResponse(HttpStatusCode.Created, "Book created, value in the response body with id updated")]
    [SwaggerWcfResponse(HttpStatusCode.BadRequest, "Bad request", true)]
    [SwaggerWcfResponse(HttpStatusCode.InternalServerError,
        "Internal error (can be forced using ERROR_500 as book title)", true)]
    public Book CreateBook(Book value)
    {
        // [.......]
    }
    
    // [.......]
}

Step 7: Decorate data types used in WCF services

[DataContract(Name = "book")]
[Description("Book with title, first publish date, author and language")]
[SwaggerWcfDefinition(ExternalDocsUrl = "http://en.wikipedia.org/wiki/Book", ExternalDocsDescription = "Description of a book")]
public class Book
{
    [DataMember(Name = "id")]
    [Description("Book ID")]
    public string Id { get; set; }

    // [.......]
}

Note: make sure you add at least the DataContract and DataMember attributes in classes and properties

Attributes

Attribute Used in Description Options
SwaggerWcf Class, Interface Enable parsing WCF service ServicePath
SwaggerWcfHidden Class, Method, Property, Parameter Hide element from Swagger
SwaggerWcfTag Class, Method, Property, Parameter Add a tag to an element TagName, HideFromSpec
SwaggerWcfHeader Method Configure method HTTP headers Name, Required, Description, DefaultValue
SwaggerWcfPath Method Configure a method in Swagger Summary, Description, OperationId, ExternalDocsDescription, ExternalDocsUrl, Deprecated
SwaggerWcfParameter Parameter Configure method parameters Required, Description, ParameterType
SwaggerWcfProperty Property Configure property parameters Required, Description, Minimum, Maximum, Default, ...
SwaggerWcfResponse Method Configure method return value Code, Description, EmptyResponseOverride, Headers
SwaggerWcfDefinition Class Configure a data type ExternalDocsDescription, ExternalDocsUrl
SwaggerWcfReturnType Method Override method return type ReturnType
SwaggerWcfContentTypes Method Override consume/produce content-types ConsumeTypes, ProduceTypes
SwaggerWcfSecurity Method Add security background to this method SecurityDefinitionName, params Scopes

Tags

Tags are used to create categories in Swagger UI.

In SwaggerWcf they can also be used to hide or show elements from the Swagger output using the configuration file.

Using the configuration from step 4, any elements with the tag LowPerformance will be hidden from Swagger.

When a SwaggerWcfTag is added to an element, it may be configured with HideFromSpec. This will prevent this tag to be displayed in the Swagger output.

When combined with SwaggerWcfHidden, if the tag has the value visible as true in web.config file, the element will be visible

Query Parameter

To specify query parameters to a function you may use the following syntax

[WebGet(UriTemplate = "/books?filter={filter}", BodyStyle = WebMessageBodyStyle.Bare)]
Book[] ReadBooks(string filter = null);

Optional Parameters

To specify a paramter as optional for swagger-ui provide a default value for the parameter on the interface.

public string Foo(string bar = null);

Optional Properties

To mark a property as optional or required, use the IsRequired parameter on the DataMember attribute.

TODO

  • Add some options to configuration in Web.config
  • Tests

How to Improve It

Fork this project abelsilva/swaggerwcf and submit pull requests.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].