Disable null validation in ASP.Net 6 project - asp.net-core-6.0

I have a really simple ASP.Net WebAPI project created in .Net 6. Given this controller method:
[HttpPost]
public async Task DoStuff(MyClass input)
{
// snip
}
where MyClass looks like this:
public class MyClass
{
public string MyData { get; set; }
}
Posting this to the DoStuff method used to be allowed in previous versions of ASP.Net:
{
MyData: null
}
Now however, it gives a 400 response unless I declare MyData as a string? instead of a string. My problem is that the MyClass class can not be altered, so I can't update MyData to be of type string?. Is there a way to disable the automatic null validation that ASP.Net does on MyClass properties? Adding <Nullable>disable</Nullable> to the csproj file for the WebAPI project doesn't seem to do anything. My current csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Platforms>x64</Platforms>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>1701;1702;1591</NoWarn>
<UserSecretsId>MyProject</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AnotherProject.csproj" />
</ItemGroup>
</Project>

This is a feature of C# 8, nullable reference types and in .NET 6 they are enabled by default. To turn it off, in your csproj file add this line <Nullable>disable</Nullable>. For example:
<PropertyGroup>
<Nullable>disable</Nullable>
</PropertyGroup>
Also, if MyClass is in another project, add <Nullable>disable</Nullable> there as well.

If you disable nullable in your csproj, it may cause warnings when you use the nullable operator(?). Adding the following code to your .editorconfig will allow you to use both methods (nullable or without nullable) without any additional warnings.
You can edit your .editorconfig file in your solution project.
Added these lines at the end of section [*.{cs,vb}]
#### Naming styles ####
[*.{cs,vb}]
# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
dotnet_diagnostic.CS8618.severity = none
# CS8601: Possible null reference assignment.
dotnet_diagnostic.CS8601.severity = none

Just another approach, if you do use nullable variables/properties/parameters, you will receive another warning when setting the nullable to disable.
To prevent it, and also disable the nullable warnings, I recommend to use the option "ANOTATIONS", this will show warnings when you specify that something is nullable and will not asume it:

Related

Groovy Griffon with Hibernate4 plugin class not found exception

I was looking for a fast and easy way to write a very cross platform desktop application. This leads me to thinking that the JVM is the place to be. Since Groovy (Grails) is used in my workplace I thought I would try Griffon since they claim it is essentially Grails for the desktop.
I wanted a persistence management layer and it doesn't not appear that GORM is showtime ready in this environment so I moved towards hibernate using the Hibernate4 plugin for Griffon.
Not that I've really used Hibernate in general however I believe, based on the guides, that I am doing things correctly. My gatherings indicate that this doesn't support annotations to wire up classes so I am using hbm.xml files.
The provided sample for the plugin isn't complex but I don't understand where I am deviating.
Here is a sample class file as it stands:
package gwash
import groovy.beans.Bindable
class DeliveryMethodModel {
// #Bindable String propName
}
Here is some of the stack trace:
org.hibernate.InvalidMappingException: Could not parse mapping document from res
ource gwash\DeliveryMethod.hbm.xml
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Con
figuration.java:3415)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueu
e(Configuration.java:3404)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(C
onfiguration.java:3392)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:
1341)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav
a:1737)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav
a:1788)
at org.hibernate.cfg.Configuration$buildSessionFactory.call(Unknown Sour
ce)
at griffon.plugins.hibernate4.Hibernate4Connector.connect(Hibernate4Conn
ector.groovy:72)
at griffon.plugins.hibernate4.Hibernate4Connector.connect(Hibernate4Conn
ector.groovy)
at griffon.plugins.hibernate4.Hibernate4Connector$connect.call(Unknown S
ource)
at Hibernate4GriffonAddon.addonInit(Hibernate4GriffonAddon.groovy:27)
at griffon.core.GriffonAddon$addonInit.call(Unknown Source)
at griffon.core.GriffonAddon$addonInit.call(Unknown Source)
at org.codehaus.griffon.runtime.util.AddonHelper.handleAddon(AddonHelper
.groovy:155)
at org.codehaus.griffon.runtime.util.AddonHelper.handleAddonsAtStartup(A
ddonHelper.groovy:105)
at org.codehaus.griffon.runtime.core.DefaultAddonManager.doInitialize(De
faultAddonManager.java:33)
at org.codehaus.griffon.runtime.core.AbstractAddonManager.initialize(Abs
tractAddonManager.java:101)
at org.codehaus.griffon.runtime.util.GriffonApplicationHelper.initialize
AddonManager(GriffonApplicationHelper.java:320)
at org.codehaus.griffon.runtime.util.GriffonApplicationHelper.prepare(Gr
iffonApplicationHelper.java:123)
at org.codehaus.griffon.runtime.core.AbstractGriffonApplication.initiali
ze(AbstractGriffonApplication.java:221)
at griffon.swing.AbstractSwingGriffonApplication.bootstrap(AbstractSwing
GriffonApplication.java:74)
at griffon.swing.AbstractSwingGriffonApplication.run(AbstractSwingGriffo
nApplication.java:131)
at griffon.swing.SwingApplication.main(SwingApplication.java:36)
Caused by: org.hibernate.PropertyNotFoundException: field [id] not found on gwas
h.DeliveryMethodModel
at org.hibernate.property.DirectPropertyAccessor.getField(DirectProperty
Accessor.java:182)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectProperty
Accessor.java:189)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectProperty
Accessor.java:189)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectProperty
Accessor.java:189)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectProperty
Accessor.java:189)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectProperty
Accessor.java:174)
at org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropert
yAccessor.java:197)
at org.hibernate.internal.util.ReflectHelper.getter(ReflectHelper.java:2
53)
at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(Refl
ectHelper.java:229)
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.
java:326)
at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:449)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBi
nder.java:382)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:322)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:173)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Con
My xml mapping file:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="gwash">
<class name="DeliveryMethodModel" table="[DELIVERY METHODS]">
<id name="id" column="[DELIVERY METHOD ID]">
<generator class="increment"/>
</id>
<property name="method" column="[DELIVERY METHOD]"/>
</class>
</hibernate-mapping>
EDIT: I've removed the brackets and spaces as indicated. Changed the DataSource.groovy to 'create' on the DB side. Still experiencing the same issues. The examples for hibernate integration with griffon/hsqldb/groovy are scant on details. Do I need to create all given properties for the model files for this to parse correctly? I've never used hibernate. Nor groovy. Nor griffon. I would definitely provide feedback for the community if I can get this resolved, if not I'll be rolling me own ORM since this is a rather small project. Rather not roll me own.
do you actually have the strings wrapped with [and ]?
I would suspect that for a class defined as
package gwash
import groovy.beans.Bindable
class DeliveryMethodModel {
Long id
#Bindable String method
}
the mapping file would be
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="gwash">
<class name="DeliveryMethodModel" table="DELIVERY_METHODS">
<id name="id" column="DELIVERY_METHOD_ID">
<generator class="increment"/>
</id>
<property name="method" column="DELIVERY_METHOD"/>
</class>
</hibernate-mapping>

Bean Autowiring problem

I am starter in mutithreading. I am trying to index my data into solr.For that I was writing the following code
I am getting null pointer exception in the line highlighted
You need to add the following:
<context:annotation-config/>
You need to set the path for autowiring package scan and in your case it will be:
<context:component-scan base-package="a.b.c" />
After it you need to mark the class as candidate for autowiring:
#Component("indexTask")
#Scope("prototype")
IndexTask implements Callable<IndexObject>
{
//ommited
}
Next you can remove indexTask bean configuration from xml file. your package will be created automatically.
Hope it helps.
Autowiring doesn't happen automatically, you need to configure it. See the Spring docs for detail, but essentially you need to add
<context:annotation-config/>

How to retrieve data from an XML file in a Sharepoint 2010 Feature Event Receiver?

I am following this tutorial and I am trying to setup the code in a Event Receiver.
I need 2 properties to send into their method a SPWeb and string.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// is there a way to make this non hardcoded?
SPSite site = new SPSite("http://localhost.com");
SPWeb web = site.OpenWeb("/");
string XMlPath = // get xml file path
CreateGroups(web, path);
}
private void CreateGroups(SPWeb currentSite, string groupsFilename)
{
}
So I tried to use getFullPath but that did not work. I also tried to use MapPath but I did not seem to have access to that.
So how do I get XML file (I think thats what I need)?
You need to dispose of the SPSite / SPWeb object, this is usually done in a using clause.
You don't need to use an absolute path (hard code) in a feature receiver, as the feature is already web/site scoped
your XmlPath usually needs to point to a file on the Sharepoint server, which you also deployed in your Feature - as the feature receiver is running after all the normal files have been deployed, you're good.
Without further ado, slightly different code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//Web scoped feature?
//spWeb = (SPWeb) properties.Feature.Parent;
//assuming Site scoped feature
spWeb = ((SPSite) properties.Feature.Parent).RootWeb;
using (spWeb)
{
string XmlPath = properties.Definition.RootDirectory + #"\Xmlfile\groups.xml"
CreateGroups(spWeb, XmlPath);
}
}
So how do you get your XML file into "\Xmlfile\groups.xml"? Just create a module! (Add new item > Module)
The elements.xml of your module should look something like this:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="Xmlfile" Path="Xmlfile">
<File Path="groups.xml" Url="Xmlfile/groups.xml" />
</Module>
</Elements>
Of course you will need to add your groups.xml file into that module (Context menu > Add existing item) for this to work.
Also note that you can easily debug feature receivers, just make sure that the deployment configuration is set to "No Activation" (Project Properties > Sharepoint > Active Deployment Configuration) - this way you will need to manually activate the feature on the site (instead of Visual Studio doing it automatically for you in debug mode) - but debugging will work flawlessly.

How do I tell ReSharper that an attribute means that a method is used?

I'm playing with SpecFlow, and ReSharper thinks that my step definitions are unused (I guess because they're used via reflection):
[Binding]
public class StepDefinitions
{
// ...
[When(#"I press add")]
public void WhenIPressAdd() // R# thinks this is unused
{
_calculator.PressAdd();
}
// ...
}
How can I tell ReSharper that methods with [Given], [When], [Then] attributes (etc.) are actually used? I don't want to use // ReSharper disable UnusedMember.Global comments.
I could also mark each method (or the whole class) with [JetBrains.Annotations.UsedImplicitly]. I don't particularly want to do that either.
You need to use JetBrains Annotations, and mark the attribute with an MeansImplicitUseAttribute. You can either reference JetBrains.Annotations.dll directly, or you can copy the annotations source code (from ReSharper / Options / Code Inspection / Code Annotations) into your solution.
If you need to annotate some external assembly you don't own, you need to create an External Annotation file (xml) in the following folder: %ReSharperInstallDir%\Bin\ExternalAnnotations. There are plenty of examples, you can just copy some.
The external annotations file can also be in the same path as the DLL if you name it DllNameWithoutExtension.ExternalAnnotations.xml.
There are plenty of examples, but I wanted to be a little more explicit in case you don't want to track down an example. :)
Create a file with the name of the attribute's assembly (.xml) in %ReSharperInstallDir%\Bin\ExternalAnnotations. For example, I made Microsoft.VisualStudio.QualityTools.CodedUITestFramework.xml and put this XML inside it:
<assembly name="Microsoft.VisualStudio.QualityTools.CodedUITestFramework">
<member name="T:Microsoft.VisualStudio.TestTools.UITesting.CodedUITestAttribute">
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
</member>
</assembly>
Restart VS and you're on your way!
these answers have helped but note worthy if you are looking to decorate an interface you will want to use the UsedImplicitly attribute
[UsedImplicitly]
public interface ISomeInterface
{
//... stuff
}

Extending SubSonic Generator

Is it possible to extend the SubSonic generator without modifying it's code?
I would like to add my own custom methods that i can use inside the templates. Somthing similair like the Utility.GetVariableType method.
You can't extend the built in templates, but you can replace them with your own templates without changing SubSonic.dll. See the templateDirectory parameter here: http://subsonicproject.com/docs/Generated_Classes/#Customizing_Active_Record
An example configuration would be:
<SubSonicService defaultProvider="Northwind" enableTrace="true"
templateDirectory="C:\Program Files\SubSonic\SubSonic 2.0.3\Templates\MVC">
<providers>
<clear/>
<add name="Northwind" type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="Northwind" generatedNamespace="Northwind"/>
</providers>
</SubSonicService>
You can get the current version of the built-in ActiveRecord templates from here.
I've found the solution for my own problem :).
I can now extend SubSonic with functionality i need in the templates without needing to rebuild or change any of the SubSonic code itself.
It works for what i wanted to do and i think it can be usefull for others as well so here it is:
Create a new class library SubSonicHelper. Mine has a class looking like this:
using System;
using System.Collections.Generic;
using System.Text;
namespace Helpers.SubSonic
{
public class GeneratorHelper
{
public bool IsColumnAllowed(string columnName)
{
return columnName.Length == 1 ||
(columnName.Length > 1 &&
(!(columnName[0].ToString().Equals("_") &&
columnName[columnName.Length - 1].ToString().Equals("_"))))
}
}
}
Build the assembly and copy SubSonicHelper.dll to your subsonic project.
Setup your SubSonic project to use your own templates using the templateDirectory parameter.
Edit your own templates and at the following after the const bool showGenerationInfo = false;
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFile(
System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "SubSonicHelper.dll"));
object instance = a.CreateInstance("Helpers.SubSonic.GeneratorHelper");
Type type = instance.GetType();
After this you have an instance of the GeneratorHelper that you can use inside the template. For accessing the methods you need to do the following:
Create an array of objects for the parameters of the method you want to use. I have the columnName parameter which i set to col.propertyName. This is inside the foreach (TableSchema.TableColumn col in cols) loop in the Update method.
Call the method you want to use with the object array as argument.
Check the result object to see the result of the method.
object[] arg = new object[]{col.PropertyName};
object isColumnAllowedResult = type.InvokeMember("IsColumnAllowed", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, instance, arg);
if (Convert.ToBoolean(isColumnAllowedResult))
That's it! Now i can extend the SubSonicHelper class with other methods i want to use inside the template.
The short answer is no. If you come up with something useful, submit a patch and it will likely be integrated into the core. You can submit patches here: http://code.google.com/p/subsonicproject/issues/list
can you not import a dll in the template?
like
<%# Import namespace="NewHelpers.Utilities"%>
and then call the function or create an instance of the object

Resources