Property marked with JsonIgnore is still shown in Swagger UI documentation for Web API - asp.net-web-api

I'm using Swashbuckle package which integrates swagger with Web API project. I want to hide the property marked as Ignored in the documentation. I tried to use different ways such as IgnoreDataMember, DataContract & DataMember, JsonIgnore or XmlIgnore but nothing seems to work with swagger ui.
However, in default API documentation it works as expected. This is how my model looks like:
public partial class Model : BaseSettingsModel
{
public string ReceiptTitle { get; set; }
[IgnoreDataMember]
public FieldsEnum Fields { get; set; }
public string DisplayFields { get; set; }
}

Moving from version 1.3.0 to 1.3.6 will solve this for you. At least #JsonIgnore and #XmlTransient are being respected.
For JsonIgnore you need to pull in the JSON.net NuGet package.

Related

Manually Creating an OData feed in Asp.Net Core, Using feed in Power BI

I am trying to manually write a Web Api that will serve as an OData feed. I don't need much functionality, just the ability to export data stored within Entity framework to an application such as Power BI. I only need to be able to view, so I was planning on just implementing GET requests.
I currently have a standard web api that returns back properly formatted JSON, but I am having trouble formatting this into something that I can import into Power BI as an OData Feed.
Here's a gist of what I have.
public class Report
{
public string ID { get; set; }
public string Name { get; set; }
public string UserID { get; set; }
...
}
[Route("api/[controller]")]
public class ReportController : Controller
{
...
[HttpGet("GetReports/{userID}")]
public IEnumerable<Report> GetReportsByUser(string userID)
{
return GetAllReportsByUser(userID);
}
...
}
I need something like this to work (obviously won't in the current form)
Since you are using ASP.NET Core 1 (aka ASP.NET 5), use the OData vNext package to build your OData service. There is a sample service on Github.

Doctrine: Validate Model Data using Annotations

In doctrine, is there a way that I could validate model data using annotations? Like the bellow example in c#
public class ProductMD {
[StringLength(50),Required]
public object Name { get; set; }
[StringLength(15)]
public object Color { get; set; }
[Range(0, 9999)]
public object Weight { get; set; }
}
So when the property Name is empty then it will give you error.
Unfortunately, starting from Doctrine2 there is no validation component integrated into the ORM itself anymore.
If, for example, you're using Doctrine2 with Symfony2, you can take advantage of the validation framework component by using #Assert annotations in the Doctrine entities.
If you don't use any framework, or if the framework you use does not provide a validation component, you can always use Doctrine's lifecycle callbacks to provide custom validation in #PrePersist and #PreUpdate (for more information, take a look here). In this case, there's more manual work to be done but it still sounds like a reasonable solution.

Filtering queries across $expands with WebApi2 OData

I have a WebApi 2.1 OData (v 5.1.1) service backed in Entity Framework 6.1. I'm trying to lock it down from a security standpoint, so that users can only query data that is theirs. I have everything working fine, until you get to the $expands option.
For the sake of this discussion, consider the following simplified data model:
public class Scenario
{
public Guid Id { get; set; }
public Guid CreatedById { get; set; }
}
public class Property
{
public Guid Id { get; set }
public Guid CreatedById { get; set; }
public IQueryable<Scenario> Scenarios { get; set; }
}
When I call /Properties(guid'SOMEGUID')?$expand=Scenarios, I need to be able to make sure that only Scenarios where the CreatedById = CurrentUserId are returned. This needs to happen on the server-side and not in the client-side query.
WCF Data Services had QueryInterceptors that would handle this kind of situation... what is the equivalent in WebApi 2.1 OData?
Thanks!
here's a gist with a sample on how can you implement this on your own:
https://gist.github.com/anonymous/9237151
Based on my git, you can use a similar validator and implement your validation logic on a CanAcess method or similar. Let me know if this helps you.
We will have soon an official sample on http://aspnet.codeplex.com
There are two ways to solve your problem if I understood your question correctly.
Call the ApplyTo method of ODataQueryOptions on the IQueryable result
public IQueryable<Property> Get(ODataQueryOptions queryOptions)
{
....
return queryOptions.ApplyTo(properties);
}
Add attribute Queryable on the GetData method and let WebAPI handles the query option
[Queryable]
public IQueryable<Property> Get()
{
...
return properties;
}

using i18n.DataAnnotations . How exactly does it work?

I am implementing a multilingual site with asp.net mvc 3.
A friend suggested me this project https://github.com/danielcrenna/i18n
Until now i have succeded to make it work in controllers and views (razor) but not in data annotation.
For example
public class LogOnModel
{
[i18n.DataAnnotations.Email]
[Required(ErrorMessage = "Required Field")]
[i18n.DataAnnotations.DataType(DataType.EmailAddress)]
public string Email { get; set; }
[i18n.DataAnnotations.Required(ErrorMessage = "Required Field")]
[DataType(DataType.Password)]
[i18n.DataAnnotations.Display(Name = "Password")]
public string Password { get; set; }
[i18n.DataAnnotations.Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
The above piece of code should produce new entries in .po files , whenever the build is successful. But nothing happens whatsoever.
Is there anybody that has knowledge of this?
Thanks in advance!
I can't comment on the i18n framework you're using from GitHub, but if it helps, there is something similar in .NET. The way you do this in .NET isn't based on .po but on resources compiled into satellite assemblies.
For MVC you can use the DisplayAttribute to specify the type which contains the resource string you wish to use. There are equivalent attributes for validation messages.
It seems that with i18n v1 is not possible to make working i18n.DataAnnotations, I asked as issue at github https://github.com/turquoiseowl/i18n/issues/104

Decorating serializable class with extra properties

I'm building a Windows Phone 7 application and I'm trying to decorate a generated class with an additional property to bind against, but I'm a bit puzzled on how to solve this architecturally. What I currently have is this class, which is generated with the xsd.exe tool from an XML file:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Session
{
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Abstract { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Speaker { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string TimeslotBegin { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string Location { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string TimeslotEnd { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute]
public string Title { get; set; }
}
I am fetching the XML from the web and deserializing this with a XmlSerializer, but I want to add an additional property to allow the user to "flag" items in the UI. I want to be able to bind to this property, so it should notify the UI thread when changed.
Any ideas on how to solve this?
For this situation, I'd recommend you separate your Model from your ViewModel.
The ViewModel is a data representation (including bindable properties) specifically designed for your UI.
The Model is the "pure" data representation, specifically designed for modelling your domain and for persistance (either directly to IsolatedStorage or perhaps persisted via a web service)
So, my recommendation is that you build some ViewModel classes for your UI to bind to - and then work out how these ViewModels interact with the Model.
As an aside, I'd also be cautious about using the XSD generated classes within Windows Phone 7 - WP7 seems to prefer the XDocument Linq XML classes, rather than the XmlDocument XML classes (but I may have this wrong!)

Resources