protected RamlDocument GetRaml(RamlVersion ramlVersion = RamlVersion.Version1, string title = null) { if (string.IsNullOrWhiteSpace(title)) { title = "Api"; } var raml = new RamlDocument { Title = title, BaseUri = baseUri, RamlVersion = ramlVersion }; var resourcesDic = new Dictionary <string, Resource>(); var parameterDescriptionsDic = new Dictionary <string, IList <ApiParameterDescription> >(); foreach (var api in apiExplorer.ApiDescriptionGroups.Items) { foreach (var apiDescription in api.Items) { var relativeUri = !apiDescription.ActionDescriptor.AttributeRouteInfo.Template.StartsWith("/") ? "/" + apiDescription.ActionDescriptor.AttributeRouteInfo.Template : apiDescription.ActionDescriptor.AttributeRouteInfo.Template; if (relativeUri.Contains("{controller}")) { relativeUri = relativeUri.Replace("{controller}", apiDescription.ActionDescriptor.RouteValues["controllerName"]); if (relativeUri.EndsWith("/")) { relativeUri = relativeUri.Substring(0, relativeUri.Length - 1); } } foreach (var apiParam in GetParametersFromUrl(relativeUri)) { relativeUri = RemoveNonExistingParametersFromRoute(relativeUri, apiDescription, apiParam.Key); } Resource resource; if (!resourcesDic.ContainsKey(relativeUri)) { resource = new Resource { Methods = GetMethods(apiDescription, new Collection <string>()), RelativeUri = relativeUri, }; parameterDescriptionsDic.Add(relativeUri, apiDescription.ParameterDescriptions); resourcesDic.Add(relativeUri, resource); } else { resource = resourcesDic[relativeUri]; foreach (var apiParameterDescription in apiDescription.ParameterDescriptions) { parameterDescriptionsDic[relativeUri].Add(apiParameterDescription); } AddMethods(resource, apiDescription, resource.Methods.Select(m => m.Verb).ToList()); } if (SetResourceProperties != null) { SetResourceProperties(resource, apiDescription); } if (SetResourcePropertiesByAction != null) { SetResourcePropertiesByAction(resource, apiDescription.ActionDescriptor); } //if (SetResourcePropertiesByController != null) // SetResourcePropertiesByController(resource, apiDescription.ActionDescriptor.ControllerDescriptor); } } raml.Schemas = new List <IDictionary <string, string> > { Schemas }; raml.Types = RamlTypes; OrganizeResourcesHierarchically(raml, resourcesDic); SetUriParameters(raml.Resources, parameterDescriptionsDic, string.Empty); if (SetRamlProperties != null) { SetRamlProperties(raml); } if (SecuritySchemes != null) { raml.SecuritySchemes = SecuritySchemes; } if (!string.IsNullOrWhiteSpace(securityType) && securityScheme != null) { SetSecurityScheme(raml); } if (SecuredBy != null) { raml.SecuredBy = SecuredBy; } if (Protocols != null) { raml.Protocols = Protocols; } return(raml); }
public void Should_Build_Multiple_Headers_When_Many_Response_With_Headers() { var doc = new RamlDocument { Title = "test" }; var okHeaders = new List <Parameter> { new Parameter { DisplayName = "code", Type = "integer" }, new Parameter { DisplayName = "token", Type = "string" } }; var errorHeaders = new List <Parameter> { new Parameter { DisplayName = "code", Type = "integer" }, new Parameter { DisplayName = "error", Type = "string" } }; var badRequestHeaders = new List <Parameter> { new Parameter { DisplayName = "code", Type = "integer" }, new Parameter { DisplayName = "description", Type = "string" } }; var okResponse = new Response { Code = "200", Headers = okHeaders }; var errorResponse = new Response { Code = "400", Headers = errorHeaders }; var notFoundResponse = new Response { Code = "404", Headers = badRequestHeaders }; var methods = new List <Method> { new Method { Verb = "get", Responses = new[] { okResponse, errorResponse, notFoundResponse } } }; var resources = new Collection <Resource> { new Resource { RelativeUri = "movies", Methods = methods } }; doc.Resources = resources; var service = new ClientGeneratorService(doc, "test", "TargetNamespace"); var model = service.BuildModel(); Assert.AreEqual(4, model.ResponseHeaderObjects.Count); var multipleModel = model.ResponseHeaderObjects.First(o => o.Key.Contains("Multiple")).Value; Assert.AreEqual(3, multipleModel.Properties.Count); Assert.AreEqual("Models." + model.ResponseHeaderObjects.First(o => o.Key.Contains("Multiple")).Value.Name, model.Classes.First().Methods.First().ResponseHeaderType); }