'Get XSD from RESTful Web API

I have created simple RESTful Web API with automatic documentation from Swagger on Azure by using C# :

namespace WebAPIDemo.Controllers
{
    
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

It is possible to call function from Swagger.

How to get XSD schema for this service?



Solution 1:[1]

Swagger does not generate metadata as XSD - metadata is published only as JSON, following the OpenAPI specification. If you use your browser to inspect the Swagger UI page for your example website, it makes an XHR call to https://webapidemo20220427161453.azurewebsites.net/swagger/docs/v1, which returns the following JSON:

{"swagger":"2.0","info":{"version":"v1","title":"WebAPIDemo"},"host":"webapidemo20220427161453.azurewebsites.net","schemes":["https"],"paths":{"/api/Values":{"get":{"tags":["Values"],"operationId":"Values_Get","consumes":[],"produces":["application/json","text/json","application/xml","text/xml"],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"type":"string"}}}}},"post":{"tags":["Values"],"operationId":"Values_Post","consumes":["application/json","text/json","application/xml","text/xml","application/x-www-form-urlencoded"],"produces":[],"parameters":[{"name":"value","in":"body","required":true,"schema":{"type":"string"}}],"responses":{"204":{"description":"No Content"}}}},"/api/Values/{id}":{"get":{"tags":["Values"],"operationId":"Values_Get","consumes":[],"produces":["application/json","text/json","application/xml","text/xml"],"parameters":[{"name":"id","in":"path","required":true,"type":"integer","format":"int32"}],"responses":{"200":{"description":"OK","schema":{"type":"string"}}}},"put":{"tags":["Values"],"operationId":"Values_Put","consumes":["application/json","text/json","application/xml","text/xml","application/x-www-form-urlencoded"],"produces":[],"parameters":[{"name":"id","in":"path","required":true,"type":"integer","format":"int32"},{"name":"value","in":"body","required":true,"schema":{"type":"string"}}],"responses":{"204":{"description":"No Content"}}},"delete":{"tags":["Values"],"operationId":"Values_Delete","consumes":[],"produces":[],"parameters":[{"name":"id","in":"path","required":true,"type":"integer","format":"int32"}],"responses":{"204":{"description":"No Content"}}}}},"definitions":{}}

But, if your API itself returns XML (and not JSON), then you can still describe the XML result using the OpenAPI facilities given here. It does not support XSD (so the metadata and type information is still returned as JSON), but this still does provide some basic type information for your API consumers to use.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mamift