Change NSwagStudio serialization setting to allow for Nulls - nswagstudio

Is there a way to change the settings in NSwagStudio so that when the JSON serialization setting outputs as Newtonsoft.Json.Required.AllowNull instead of Newtonsoft.Json.Required.Always?
I currently have the property manually changed to allow for nulls.
[Newtonsoft.Json.JsonProperty("returnCode", Required = Newtonsoft.Json.Required.Always)]
public int ReturnCode { get; set; }
And I need it to be:
[Newtonsoft.Json.JsonProperty("returnCode", Required = Newtonsoft.Json.Required.AllowNull)]
public int ReturnCode { get; set; }

It will not be nullable inside the generated code if it is not nullable at the API-spec
API spec example:
ReturnCode:
type: integer
nullable: true

Related

AutoMapper's AssertConfigurationIsValid and EF navigation properties

I discovered AutoMapper's Configuration Validation feature today and it looks very promising. Using it I should be able to get rid of all our manually written unit tests for our AutoMapper profiles. We use AutoMapper to map between Entity Framework entity classes and View Model classes.
Imagine I have the following entity:
public class Article
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
[ForeignKey("TypeId")]
[InverseProperty("Article")]
public ArticleType Type { get; set; }
}
And the corresponding View Model:
public class ArticleViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public string TypeName { get; set; }
}
I have left out ArticleType for brevity.
Now, in my AutoMapper profile I would have these mappings:
CreateMap<ArticleViewModel, Article>()
CreateMap<Article, ArticleViewModel>()
.ForMember(dest => dest.TypeName, options => options.MapFrom(src => src.Type.Name))
If I call AssertConfigurationIsValid on a MapperConfiguration with this profile in it AutoMapper will complain that Type is not mapped. That is true, but I do not need to map it since Entity Framework will automatically figure it out from the foreign key TypeId.
I know I can add an Ignore for Type, like below, to get rid of this error:
CreateMap<ArticleViewModel, Article>()
.ForMember(dest => dest.Type, options => options.Ignore())
But we have entities with a lot of navigation properties to other entities and having to ignore them all becomes tedious.
The other alternative I came up with is to use the source's members to validate the mapping, like this:
CreateMap<ArticleViewModel, Article>(MemberList.Source)
Is there a best practice for this?

GlassMapper SC nullable boolean fields causing Failed item resolve

We have recently upgraded from Glass.Sitecore.Mapper 2.0.12.0 to Glass.Mapper.Sc 3.3.1.53 (we can't upgrade any higher as we are using Sitecore 6).
We are having problem with saving items with nullable boolean fields
i.e.
[SitecoreType(TemplateId = Templates.Category)]
public class Category : AggregateBase
{
...
[SitecoreField(FieldName = CategoryFields.Requires360)]
public virtual bool? Requires360 { get; set; }
[SitecoreField(FieldName = CategoryFields.RequiresCatwalk)]
public virtual bool? RequiresCatwalk { get; set; }
}
Where AggregateBase contains the standard sitecore fields
e.g.
[SitecoreType]
public class AggregateBase
{
[SitecoreId]
public virtual Guid Id { get; set; }
[SitecoreInfo(SitecoreInfoType.Name)]
public virtual string Name { get; set; }
...
}
If the nullable bool field is null it saves without issue.
If the nullable field has a value we get the following exception
"Failed item resolve - You cannot save a class that does not contain a
property that represents the item ID. Ensure that at least one
property has been marked to contain the Sitecore ID. Type:
System.Boolean"
This configuration worked previously and seems to work with other nullable types e.g. int?
The underlying storage in the template field is "Single-Line Text".
Does anyone have any ideas?
Regards,
Mark

Grouping items based on last added item in set

I have an app where I store debug information from several sources. The data is stored in a class like the following:
public class DebugMessage
{
public string ApplicationName { get; set; }
public string Details { get; set; }
public string Id { get; set; }
public DateTime OccurredOn { get; set; }
public IList<string> Tags { get; set; }
public string TextMessage { get; set; }
public MessageTypes Type { get; set; }
public IDictionary<string, string> Metadata { get; set; }
public int Count {get;set;}
public bool Same(DebugMessage other){...}
}
Now, I already have setup indexes and maps/reduce for each item I need. What I would like to do now is the following:
When ADDING a new item to the collection, If this items "looks the same" (by calling the Same method on the item and passing in the last added item in the collection), I would like to just update the last added item and do not add a new item. If the items are not the same, I would like to add it to the collection.
I guess I can do this with some kind of Map/Reduce, but I can't wrap my head around this. I'm new to Raven and don't know how to do the above (or even if that's possible).
Any directions?
You need to move your in the Same method to the map/reduce index, and group based on the values that make you consider the two things to be the same.
Alternatively, query for similar debug message, and update the result.

Getting an Internal Link with Glass.Mapper

I've got an Internal Link set up in Sitecore, and I'm trying to map the field using Glass.Mapper, but it just keeps coming back empty, and I'm not sure what I'm doing wrong.
The template in Sitecore is pretty simple:
The Source of the link is set to a folder that only allows content based on the 'System' template to be created.
In my code, I have an object set up:
namespace Playground.GlassObjects
{
public partial class Status
{
public virtual string Description { get; set; }
public virtual string StatusCode { get; set; }
public virtual Glass.Mapper.Sc.Fields.Link System { get; set; }
}
}
Which is being used basically like this:
public void DoStuff(Sitecore.Data.Items.Item item)
{
var status = item.GlassCast<Status>();
this.DoOtherStuff(status);
}
What I'm running into is glassObj.Description, and glassObj.StatusCode are being wired up exactly like I want/expect, but glassObj.System is not.
Can anyone tell me what I'm doing wrong here? I'm at a loss right now, with all the magic that's going on behind the scenes.
The Glass.Mapper.Sc.Fields.Link class is designed to work with the General Link field. The internal link field stores values as paths e.g /sitecore/content/home/events. This means it isn't compatible with the Link class.
Instead you should map it to another class you have created.
public partial class Status
{
public virtual string Description { get; set; }
public virtual string StatusCode { get; set; }
public virtual MySystem System { get; set; }
}
public class MySystem{
public virtual string Url { get; set; }
public virtual string MyField { get; set; }
}
Fast forward to 2022 Internal Link field seems to be working with Glassmapper without any extra effort. All you have to do is add Internal Link case to GlassGenerator.tt file on the project where you will generate the template.
This will ensure your model will have Link field like this:
[SitecoreField(FieldId = "{D2CF138A-0A1C-4766-B250-F56E9458B624}")]
Link InternalLinkField{ get; set; }
It will have some info populated and most of the other properties will be null. The ones that will help you are:
Url (full path to the internal link item)
TargetId (ID of the internal link item)
Here are the available properties:
There is an alternative to that, you can get the link from fields like this:
yourGlassItem.Item.Fields["InternalLinkFieldName"]
You will get the entire Internal link Item. You can use Value or InheritedValue property to get the path of the linked item.

Cast objects of different type

I have wrote a class (a snippet is below) which have some data members where i put data from the Client Side. I should send this data through Web Services where is a class which includes my data members, but has more data members that my class.
I should cast from my type into another type.
The problem is that i don't know how to access data members to take data from.
all my data are into this object "OBJ":
__XtraInvoiceInfo OBJ = new __XtraInvoiceInfo();
and , the Web Services's type is "InvoiceWithEntriesInfo"
var convertedObj = new InvoiceWithEntriesInfo()
{
invoiceNumber = OBJ.BasicInfo.InvoiceNumber --> member is not access.
| Equals
Visual Studio suggests | GetHashCode
only these methods | GetType
| ToString
invoiceDate = OBJ.BasicInfo.InvoiceDate *--> member is not accessible
firstName = OBJ.Payer.FirstName *-->> not accessible
lastName = OBJ.Payer.LastName *-->> not accessible
catem = OBJ.Payer.Catem *-->> not accessible
};
error "member is not accessible" means *--> 'object' does not contain a definition for 'InvoiceDate' and no extension method 'InvoiceDate' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
public sealed class __XtraInvoiceInfo
{
private long _payerType = -1;
public long PayerType
{
get
{
return this._payerType;
}
set
{
this._payerType = value;
if (value == Constants.NATURAL_PAYER)
{
this.Payer = new __NaturalInvoiceInfo();
}
}
}
public object Payer
{
get; set;
}
public object BasicInfo
{
get; set;
}
//-- Nested Types --
public sealed class __NaturalInvoiceInfo
{
public string FirstName
{
get; set;
}
public string LastName
{
get; set;
}
public long Catem
{
get; set;
}
}
public sealed class __BasicInvoiceInfo
{
public long InvoiceNumber
{
get; set;
}
public DateTime? InvoiceDate
{
get; set;
}
}
}
I made properties Payer and BasicInfo because through them i take data from Client and i made a subbinding into my members like this way:
model.BindModel(xii =>
{
var bindModel = new ValidationFrameBindModel<__XtraInvoiceInfo.__BasicInvoiceInfo>();
this.BindControls(bindModel);
model.BindModel<__XtraInvoiceInfo.__BasicInvoiceInfo>((x,b) => x.BasicInfo = b, bindModel);
});
Thank you so much!!! if you have the power to answer my question.
I'm ready to say more details if it is required.
Well this is the problem:
public object Payer { get; set; }
public object BasicInfo { get; set; }
You're only declaring the properties as being of type object - why not give them more useful types? If you don't know the types, how do you know what properties will be there? Can you create abstract base class or interface which declares all the properties you want to guarantee will be there? (It's fairly hard to tell what you're trying to do, to be honest.)
If you're using C# 4 and .NET 4 you could just make them dynamic:
public dynamic Payer { get; set; }
public dynamic BasicInfo { get; set; }
Then accessing sub-properties will be bound at execution time against the actual type of object.
On a side-note, please don't prefix type names with __ - the C# specification reserves identifiers using __ for compiler-specific features. From section 2.4.2:
Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.