private SwaggerRouteData CreateSwaggerRouteData(INancyModule module, Route route, Dictionary <RouteId, MethodInfo> routeHandlers) { var data = new SwaggerRouteData { ApiPath = route.Description.Path, ResourcePath = module.ModulePath.EnsureForwardSlash(), OperationMethod = route.Description.Method.ToHttpMethod(), OperationNickname = route.Description.Name }; var routeId = RouteId.Create(module, route); var handler = routeHandlers.ContainsKey(routeId) ? routeHandlers[routeId] : null; if (handler == null) { data.OperationNotes = "[example]"; // TODO: Insert example how to annotate a route data.OperationSummary = "Warning: no annotated method found for this route"; data.Show = !SwaggerConfig.ShowOnlyAnnotatedRoutes;; return(data); } foreach (var attr in handler.GetCustomAttributes <RouteAttribute>()) { data.OperationSummary = attr.Summary ?? data.OperationSummary; data.OperationNotes = attr.Notes ?? data.OperationNotes; data.OperationModel = attr.Response ?? data.OperationModel; data.OperationConsumes = attr.Consumes ?? data.OperationConsumes; data.OperationProduces = attr.Produces ?? data.OperationProduces; data.Show = attr.Show != NullableBool.False; } data.OperationResponseMessages = handler.GetCustomAttributes <SwaggerResponseAttribute>() .Select(attr => { var msg = new ResponseMessage { Code = (int)attr.Code, Message = attr.Message }; if (attr.Model != null) { msg.ResponseModel = Primitive.IsPrimitive(attr.Model) ? Primitive.FromType(attr.Model).Type : SwaggerConfig.ModelIdConvention(attr.Model); } return(msg); }) .ToList(); data.OperationParameters = handler.GetParameters() .Select(CreateSwaggerParameterData) .ToList(); return(data); }
public void ToModelProperty_NonPrimitive_ShouldHaveRefSet() { new SwaggerModelPropertyData { Type = typeof(TestModel) }.ToModelProperty().ShouldEqual( new ModelProperty { Ref = SwaggerConfig.ModelIdConvention(typeof(TestModel)) } ); }
public void ToOperation_ModelIsNonPrimitive_ShouldHaveTypeSet() { new SwaggerRouteData { OperationMethod = HttpMethod.Get, OperationModel = typeof(TestModel), }.ToOperation().ShouldEqual( new Operation { Method = HttpMethod.Get, Nickname = "get", Type = SwaggerConfig.ModelIdConvention(typeof(TestModel)), Parameters = Enumerable.Empty <Parameter>() } ); }
public void ToModelPropertyNonPrimitiveCollection_ShouldHaveTypeArrayAndItemsRefSet() { new SwaggerModelPropertyData { Type = typeof(TestModel[]) }.ToModelProperty().ShouldEqual( new ModelProperty { Type = "array", Items = new Item { Ref = SwaggerConfig.ModelIdConvention(typeof(TestModel)) } }, "String return type" ); }
public void ToParameter_BodyParamWithoutName_ShouldHaveNameBody() { new SwaggerParameterData { ParamType = ParameterType.Body, ParameterModel = typeof(TestModel) }.ToParameter().ShouldEqual( new Parameter { Name = "body", ParamType = ParameterType.Body, Type = SwaggerConfig.ModelIdConvention(typeof(TestModel)), }, "If paramType is \"body\", the name is used only for Swagger-UI and Swagger-Codegen. In this case, the name MUST be \"body\"." ); }
public void ToParameter_BodyParam_ShouldHaveTypeSetToModelId() { new SwaggerParameterData { ParamType = ParameterType.Body, ParameterModel = typeof(TestModel) }.ToParameter().ShouldEqual( new Parameter { Name = "body", ParamType = ParameterType.Body, Type = SwaggerConfig.ModelIdConvention(typeof(TestModel)), }, "Type field MUST be used to link to other models." ); }
public void ToOperation_ModelIsNonPrimitiveCollection_ShouldHaveTypeArrayAndItemsRefSet() { new SwaggerRouteData { OperationMethod = HttpMethod.Get, OperationModel = typeof(TestModel[]), }.ToOperation().ShouldEqual( new Operation { Method = HttpMethod.Get, Nickname = "get", Type = "array", Items = new Items { Ref = "#/definitions/" + SwaggerConfig.ModelIdConvention(typeof(TestModel)) }, Parameters = Enumerable.Empty <Parameter>() }, "String return type" ); }
public SwaggerRoot GetSwaggerJson(NancyContext context) { var builder = new SwaggerRootBuilder(); foreach (var pathItem in this.RetrieveSwaggerPaths(context)) { builder.Path(pathItem.Key, pathItem.Value.PathItem); } builder.Info(_info); foreach (var model in RetrieveSwaggerModels()) { // arrays do not have to be defined in definitions, they are already being declared fully inline // either they should use #ref or they shouldn't be in definitions if (model.ModelType.IsContainer()) { continue; } builder.Definition(SwaggerConfig.ModelIdConvention(model.ModelType), model.GetSchema(true)); } foreach (var tag in RetrieveSwaggerTags()) { builder.Tag(tag); } foreach (var securityScheme in _securitySchemes) { builder.SecurityDefinition(securityScheme.Key, securityScheme.Value.Build()); } return(builder.Build()); }
public SwaggerRoot GetSwaggerJson(NancyContext context) { var builder = new SwaggerRootBuilder(); if (_swaggerRoot?.Host != null) { builder.Host(_swaggerRoot.Host); } if (_swaggerRoot?.BasePath != null) { builder.BasePath(_swaggerRoot.BasePath); } if (_swaggerRoot?.Schemes != null) { _swaggerRoot.Schemes.ToList().ForEach(x => builder.Scheme(x)); } if (_swaggerRoot?.Consumes != null) { builder.ConsumeMimeTypes(_swaggerRoot.Consumes); } if (_swaggerRoot?.Produces != null) { builder.ProduceMimeTypes(_swaggerRoot.Produces); } if (_swaggerRoot?.ExternalDocumentation != null) { builder.ExternalDocumentation(_swaggerRoot.ExternalDocumentation); } foreach (var pathItem in this.RetrieveSwaggerPaths(context)) { builder.Path(pathItem.Key, pathItem.Value.PathItem); } builder.Info(_info); foreach (var model in RetrieveSwaggerModels()) { // arrays do not have to be defined in definitions, they are already being declared fully inline // either they should use #ref or they shouldn't be in definitions if (model.ModelType.IsContainer()) { continue; } builder.Definition(SwaggerConfig.ModelIdConvention(model.ModelType), model.GetSchema(true)); } foreach (var tag in RetrieveSwaggerTags().OrderBy(x => x.Name)) { builder.Tag(tag); } foreach (var securityScheme in _securitySchemes) { builder.SecurityDefinition(securityScheme.Key, securityScheme.Value.Build()); } return(builder.Build()); }