private static void BuildPaths(SwaggerSchema service, IList <string> hiddenTags, List <string> visibleTags, IList <Type> definitionsTypesList) { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); service.Paths = new Dictionary <string, PathItem>(); foreach (Assembly assembly in assemblies) { IEnumerable <TypeInfo> types; try { types = assembly.DefinedTypes; } catch (Exception) { // ignore assembly and continue continue; } foreach (TypeInfo ti in types) { SwaggerWcfAttribute da = ti.GetCustomAttribute <SwaggerWcfAttribute>(); if (da == null || hiddenTags.Any(ht => ht == ti.AsType().Name)) { continue; } Mapper mapper = new Mapper(hiddenTags, visibleTags); IEnumerable <Path> paths = mapper.FindMethods(da.BasePath, ti.AsType(), definitionsTypesList); //service.Paths.AddRange(paths); } } }
internal static string Process(SwaggerSchema service) { var sb = new StringBuilder(); var sw = new StringWriter(sb); using (JsonWriter writer = new JsonTextWriter(sw)) { service.Serialize(writer); } return(sb.ToString()); }
public SwaggerSchema Build() { var attr = _serviceContractType.GetCustomAttribute <SwaggerWcfAttribute>(); SwaggerSchema swaggerSchema = new SwaggerSchema { Info = _serviceContractType.GetServiceInfo(), Host = attr.Host, BasePath = attr.BasePath, Schemes = attr.Schemes, Consumes = attr.Consumes, Produces = attr.Produces, }; return(swaggerSchema); }
private static void BuildPaths <TBusiness>(SwaggerSchema service, IList <string> hiddenTags, List <string> visibleTags, IList <Type> definitionsTypesList) { Type type = typeof(TBusiness); service.Paths = new Dictionary <string, PathItem>(); SwaggerWcfAttribute da = type.GetCustomAttribute <SwaggerWcfAttribute>(); if (da == null || hiddenTags.Any(ht => ht == type.Name)) { return; } Mapper mapper = new Mapper(hiddenTags, visibleTags); IEnumerable <Path> paths = mapper.FindMethods(da.BasePath, type, definitionsTypesList); //service.Paths.AddRange(paths); }
private static SwaggerSchema BuildServiceCommon(string path, Action <SwaggerSchema, IList <string>, List <string>, IList <Type> > buildPaths) { const string sectionName = "swaggerwcf"; SwaggerWcfSection config = (SwaggerWcfSection)(ConfigurationManager.GetSection(sectionName) ?? new SwaggerWcfSection()); List <Type> definitionsTypesList = new List <Type>(); SwaggerSchema service = new SwaggerSchema(); List <string> hiddenTags = SwaggerWcfEndpoint.FilterHiddenTags(path, GetHiddenTags(config)); List <string> visibleTags = SwaggerWcfEndpoint.FilterVisibleTags(path, GetVisibleTags(config)); IReadOnlyDictionary <string, string> settings = GetSettings(config); ProcessSettings(service, settings); buildPaths(service, hiddenTags, visibleTags, definitionsTypesList); service.Definitions = DefinitionsBuilder.Process(hiddenTags, visibleTags, definitionsTypesList); return(service); }
public static string GenerateSwaggerFile() { string path = GetAllPaths().Where(p => !SwaggerFiles.Keys.Contains(p)).Single(); SwaggerSchema service = ServiceBuilder.Build("/"); service.Info = Info; service.SecurityDefinitions = SecurityDefinitions; string swagger = Serializer.Process(service); if (SwaggerFiles.ContainsKey(path) == false) { SwaggerFiles.Add(path, swagger); } return(JsonConvert.SerializeObject(service, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); }
public SwaggerSchema Create(Type type, IEnumerable <IPropertyDetails> propertyDetails, Queue <Type> typeQueue) { if (type == null) { throw new ArgumentNullException(nameof(type)); } var schema = new SwaggerSchema { Title = type.Name, Type = _swaggerTypeConverter.GetSwaggerType(type) // TODO: Required }; if (schema.Type == SwaggerType.Object && propertyDetails != null) { schema.Properties = new SwaggerObjectCollectionFacade <SwaggerProperty>( propertyDetails.Select(p => _propertyFactory.Create(p, typeQueue))); } return(schema); }
internal static void Init(Func <string, SwaggerSchema> buildService) { string[] paths = GetAllPaths().Where(p => !SwaggerFiles.Keys.Contains(p)).ToArray(); foreach (string path in paths) { SwaggerSchema service = buildService(path); if (Info != null) { service.Info = Info; } if (SecurityDefinitions != null) { service.SecurityDefinitions = SecurityDefinitions; } string swagger = Serializer.Process(service); if (SwaggerFiles.ContainsKey(path) == false) { SwaggerFiles.Add(path, swagger); } } }
private static void ProcessSettings(SwaggerSchema service, IReadOnlyDictionary <string, string> settings) { if (settings.ContainsKey("BasePath")) { service.BasePath = settings["BasePath"]; } if (settings.ContainsKey("Host")) { service.Host = settings["Host"]; } //if (settings.ContainsKey("Schemes")) // service.Schemes = settings["Schemes"].Split(';').ToList(); if (settings.Keys.Any(k => k.StartsWith("Info"))) { service.Info = new Info(); } if (settings.ContainsKey("InfoDescription")) { service.Info.Description = settings["InfoDescription"]; } if (settings.ContainsKey("InfoVersion")) { service.Info.Version = settings["InfoVersion"]; } if (settings.ContainsKey("InfoTermsOfService")) { service.Info.TermsOfService = settings["InfoTermsOfService"]; } if (settings.ContainsKey("InfoTitle")) { service.Info.Title = settings["InfoTitle"]; } if (settings.Keys.Any(k => k.StartsWith("InfoContact"))) { service.Info.Contact = new InfoContact(); } if (settings.ContainsKey("InfoContactName")) { service.Info.Contact.Name = settings["InfoContactName"]; } if (settings.ContainsKey("InfoContactUrl")) { service.Info.Contact.Url = settings["InfoContactUrl"]; } if (settings.ContainsKey("InfoContactEmail")) { service.Info.Contact.Email = settings["InfoContactEmail"]; } if (settings.Keys.Any(k => k.StartsWith("InfoLicense"))) { service.Info.License = new InfoLicense(); } if (settings.ContainsKey("InfoLicenseUrl")) { service.Info.License.Url = settings["InfoLicenseUrl"]; } if (settings.ContainsKey("InfoLicenseName")) { service.Info.License.Name = settings["InfoLicenseName"]; } }
public SwaggerArray(string name, SwaggerSchema items) { base.Nome = name; base.Type = "array"; this.ItemsType = items; }