How do I allow null properties on my generated POCO classes? - njsonschema

Using NJsonSchema.CodeGeneration, I'm able to output the properties as-defined in JSON schema. However, I noticed that in the generated, code, a couple of things are going on:
all of the properties have the Required = Newtonsoft...DisallowNull property defined.
each property is set to a new instance of a class.
Example:
[Newtonsoft.Json.JsonProperty("myProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public MyProperty MyProperty { get; set; } = new MyProperty();
Question:
How do I make it so that the generated code allows nulls? These are not required properties in the JSON schema, and it doesn't make sense to initialize them. On the other hand, it is handy to have the collection classes initialized by default, so they can be iterated without throwing an exception (this is the current behavior for collections as well).

Related

Custom serialization of specific return type for DataFetcher

I am using Spring for GraphQL (version 2.7.0-M1).
In my domain model, a lot of properties return an object Foo. This object must be serialized to a String based on data available from GraphQlContext. So the schema looks like:
type Parent {
code: String!
foo: String
...
}
It is easy to do this with #SchemaMapping for a specific parent type.
#SchemaMapping(typeName = "Parent", field = "foo")
public String foo(Parent parent, DataFetchingEnvironment env) {
var context = env.getGraphQlContext();
return ...
However, this is not very DRY. I am looking for a way to have this code at one place, like a custom scalar.
Is there a way to do this with spring-graphql / graphql-java?
Example
An example is a Localized<T> object we use. For instance a Product instance has Localized<String> properties for title and description (and more).
For the GraphQL query we can set the context, part of the context is the Locale. For all Localized property values the value can be converted to the string value for the locale. We are looking for a way to do this automagically. Otherwise it creates a lot of boiler plate code
Would #ContextValue help here? This would remove a bit of boilerplate from your controller handlers.
#SchemaMapping(typeName = "Parent", field = "foo")
public String foo(Parent parent, #ContextValue Foo foo) {
If you'd like something more involved, I think you should elaborate on the exact relationship between an attribute of one or multiple types in your schema, and some random value in the context.
Maybe you could come up with some concrete example here?

linq entity framework include weird behaviour

I have an issue with the linq query when I dont explicitly use include for a navigation property.
---Task entity model ----
...
public Project Project { get; set; }
public TaskType TaskType { get; set; }
...
var tasks = db.Tasks
.Where(t => (t.Project.ProjectId == project.ProjectId))
foreach (Task t in tasks)
{
// for some items t.TaskType is null
// I have noticed that when t.TaskType.Id
// is different to the first one in the loop then only the next different
// tasktype is null
}
// however when using
var tasks = db.Tasks.Include(t=>t.TaskType)
.Where(t => (t.Project.ProjectId == project.ProjectId))
// tasktype is always there (where as without the include in some tasks it does not extract the tasktype)
Using the .Include fixed my issue but I like to understand why it behaves like this.
If you use Eager Loading in EF, then navigation properties not load with root object. Include method instruct EF, that navigation property should loaded with your root object.
With Lazy loading your navigation property loaded, when you call this property, so if you use lazy load, then Include method not necessary.
More here
I think what you looking for is lazy load the related entities. To do that you need to specify your navigation properties as virtual. From this page:
Lazy loading is the process whereby an entity or collection of
entities is automatically loaded from the database the first time that
a property referring to the entity/entities is accessed. When using
POCO entity types, lazy loading is achieved by creating instances of
derived proxy types and then overriding virtual properties to add the
loading hook.
When you specify the virtual keyword in your navigation properties, EF creates at runtime dynamic proxies for your entity classes. Those proxy classes are responsible for the lazy loading behavior of the related entities. Without virtual, lazy loading will not be supported, and you get null on your navigation properties. So you need to do this:
public class Task
{
//...
public virtual Project Project { get; set; }
public virtual TaskType TaskType { get; set; }
{
In this link you will find all the requiriments you need to follow to support lazy loading.
Using the Include method you can eager load the related entities as part of the query.

Few questions... ModelState.IsValid and Grouped CheckBox Values

Using ASP.NET MVC when I create my model, then a controller based on the model with CRUD operations, the CRUD views are generated. I added some code using Fluent API to require certain fields but for some reason the ModelState.IsValid passes even when these fields are not completed. What determines whether this passes or not? I thought it was based on your model property data types and other things like being required or maxlength, etc....
Also, I have manually added code to grab a list of Categories from the database and generate a checkbox for each one in the View. This is a navigation property for the Project model where there is a many-many relationship. To get the group of checked values in the Create(Project project) method in the controller I use:
var selected = Request["categories"].Split(',');
This however, throws the classic Object reference not set to an instance of an object error if no values are checked. So what I want to know is, how can I determine that this does not have any values so I can do something else once detected?
I added some code using Fluent API to require certain fields but for
some reason the ModelState.IsValid passes even when these fields are
not completed.
ASP.NET MVC doesn't know anything about the Fluent API of Entity Framework and doesn't evaluate this configuration. You only can use the data annotations which MVC will recognize:
[Required]
public string SomeProperty { get; set; }
...how can I determine that this does not have any values so I can do
something else once detected?
Not sure if I understand it correctly but I'd say:
var categories = Request["categories"];
if (categories != null)
{
var selected = categories.Split(',');
// ...
}
else
{
// do something else
}

Unable to Save IsolatedStorageSettings.ApplicationSettings for wp7

if (settings.Contains("myDetailsObject"))
{
settings["myDetailsObject"] = myDetails;
}
else
{
settings.Add("myDetailsObject", myDetails);
}
settings.Save();
Tried doing the below, however it gave me error. those save values are in strings and is a custom object. tried even saving an integer instead and is still not working
Type 'SharedLibary.Object.MyDetailsObject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
Add the attribute [DataMember] on all properties that you want to serialize in your MyDetailsObject class.
Mark class with [DataContractAttribute] attribute and all members that you want to serialize with [DataMemberAttribute]. Note, that marked properties must be public.
Also, don't forget to add reference to System.Runtime.Serialization

In Spring MVC 3, how do I bind an object to a query string when the query string parameters don't match up with the object fields?

A 3rd party is sending me part of the data to fill in my domain object via a query string. I need to partially fill in my domain object, and then have the user fill in the rest via a form. I don't have any control over the query string parameters coming in, so I can't change those, but I'd really like to be able to use Spring MVC's data binding abilities, rather than doing it by hand.
How can I do this?
To add some complication to this, some of the parameters will require extensive processing because they map to other objects (such as mapping to a user from just a name) that may not even exist yet and will need to be created. This aspect, I assume, can be handled using property editors. If I run into trouble with this, I will ask another question.
Once I have a partially filled domain object, passing it on to the edit view, etc. is no problem, but I don't know how to properly deal with the initial domain object population.
The only thing I have been able to come up with so far is to have an extra class that has it's properties named to match the inbound query parameters and a function to convert from this intermediary class to my domain class.
This seems like a lot of overhead though just to map between variable names.
Can you not just have the getter named differently from the setter, or have 2 getters and 2 setters if necessary?
private int spn;
// Standard getter/setter
public int getSpn() {
return spn;
}
public void setSpn(int spn) {
this.spn = spn;
}
// More descriptively named getter/setter
public int getShortParameterName() {
return spn;
}
public void setShortParameterName(int spn) {
this.spn = spn;
}
Maybe that is not standard bean convention, but surely would work?

Resources