2

I have JSON schema for a service implementation, but the manual implementation is time consuming.

Is there any way or tool for generating service interfaces and client code from JSON schema? Like we do for a WSDL document, we can easily create service and client code.

This is from OCPP 1.6 JSON Specification

Sample schema file:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "AuthorizeRequest",
    "type": "object",
    "properties": {
        "idTag": {
            "type": "string",
            "maxLength": 20
        }
    },
    "additionalProperties": false,
    "required": [
        "idTag"
    ]
}
Shahzad
  • 123
  • 2
  • 11
  • I updated my question with a sample json schema format. I am not really sure what format your are talking about. don't know much about json stuff though. btw I found a tool NSwagStudio, i am about to test it, it does have Csharp client and web api controller generator it seems – Shahzad Apr 13 '16 at 09:19
  • NSwagStudio generated Swagger specification json is not valid with Swagger codegen :( – Shahzad Apr 13 '16 at 10:49

2 Answers2

2

WSDL is only compatible with the SOAP 1.1/1.2 specification for web services.

The nice thing about consuming SOAP services with a WSDL metadata description is that, as you say, you can just generate the client code necessary to call the service. This is because the WSDL file specifies the following things:

  • Address - where the service endpoint is located
  • Bindings - what transport you should use to consume the service
  • Contract - the service operations, including any custom types definitions

While there are equivalents for WSDL with a REST service, they will only be available to you if the service provider has implemented them. An example is WADL.

When consuming a REST service you must assemble the above three pieces of information manaually, as you have discovered. The equivalent information for a REST service:

  • Address - The base service URL + the resource URL
  • Binding - HTTP - GET/POST/PUT/DELETE
  • Contract - The HTTP verb supported by the resource, and the JSON (or XML) representation of the resource.

For the address, you can get this from the service provider.

The binding can be acheived by using WebClient or HttpClient. (WebClient is the easiest to use, HttpClient is shinier)

The Contract, you will need to ask the service provider what verbs are supported by the resource you want to consume. Then you can use something like the json2csharp online tool to generate your client side type representation of the resource JSON.

Once you have put all this in place you should be ready to consume the new service.

Community
  • 1
  • 1
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • Thanks @TomRedfern for breaking it down. but there is no way i can ask for what verbs are supported . This service specification schema is all i have though.may be i am not understanding your suggestion exactly, newbie here! – Shahzad Apr 13 '16 at 09:36
  • @Shahzad - well without knowing how to make the http call, I can't see how you can call the operation. I am not so familiar with json-schema. – tom redfern Apr 13 '16 at 09:38
  • 1
    @Shahzad it looks like the operation you need to call is a POST. So you can use the json schema to generate a c# class representation of the call payload and then call the service as I have descibed above. – tom redfern Apr 13 '16 at 09:44
2

Yes there is a standard called Swagger which is a JSON based based standard for defining RESTful JSON based API's.

There is an open source library for implementing it for ASP.NET Web API JSON API's known as Swashbuckle.

There are many tools that currently support it and the ecosystem is growing very fast. There are SDK generators based on Swagger definitions and Microsoft Azure API Service supports integrating with Swagger compliant API's.

Although it does not implement the entire JSON Schema standard it is a very powerful tool for sharing and implementing JSON API's.

Gabriel Sadaka
  • 1,748
  • 1
  • 15
  • 19
  • Hmm it would appear my answer is a little behind the times. I would guess that the service provider has probably implemented a metadata standard of some kind for them to be able to provide the OP with a schema based on http://json-schema.org/. Does the tooling you have mentioned work with that? – tom redfern Apr 13 '16 at 09:24
  • 1
    Yes and no, it implements the majority of it but there are parts the implementers of Swagger avoided from the json-schema standard. It mentions parts of what it supports here http://swagger.io/specification/ – Gabriel Sadaka Apr 13 '16 at 09:27
  • isn't Swashbuckle for describing swagger from WebAPI ? – Shahzad Apr 13 '16 at 09:42
  • Yes that is exactly what it is for. It will generate a swagger document for your WebAPI based on all of your controllers and their actions. Then you can use Swagger tools to generate SDK's for them in a variety of languages. – Gabriel Sadaka Apr 13 '16 at 09:48
  • Do you know if they are capable of providing a Swagger compliant schema for it? – Gabriel Sadaka Apr 13 '16 at 09:51
  • No, they wont do that for a single client. – Shahzad Apr 26 '16 at 14:44