Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeTypeModel.CanSerializeContractType has some counter-intuitive behavior. #701

Open
alennartz opened this issue Aug 17, 2020 · 5 comments

Comments

@alennartz
Copy link

Hey,

I'm observing some counter intuitive behavior with regards to what types are reported as serializable. Not sure if this is by design or not so here goes.

What I'm trying to accomplish

Obtain an instance of a RuntimeTypeModel that will return false on bool CanSerializeContractType(Type t) unless that type has been explicitely annotated by a ProtoContractAttribute or is a List of such types. Now I can obviously just do the attribute check myself but I was expecting that I could configure an instance to basically behave in this way.

What I have tried:

var strictModel = RuntimeTypeModel.Create("strict");
strictModel.AutoAddProtoContractTypesOnly = true;
strictModel.InferTagFromNameDefault = false;

What I have observed

Some types even when not annotated by the attribute will come back as being supported by the type model. I have not been super exhaustive in my testing but what i have observed is that the types that seem to magically work are types that have get only properties where each of those properties can be found as a constructor parameter and that the constructor parameter has the same name (case insensitive) as the property.

The type definitions I have tested with

public class TypeA
{
	
}

public class TypeB
{
	public int Value { get; }
	
	public TypeB(int value)
	{
		
	}
}

public class TypeC
{
	public int Value { get; }
	
	public TypeC(int notValue)
	{
			
	}
}

public class TypeD
{
	public int Value { get; set; }

	public TypeD(int value)
	{

	}
}

public class TypeE
{
	public int Value { get; set; }

	public TypeE()
	{

	}
}

Out of all these types TypeB is the only one that returns true.

The Questions

  • Is this intended behavior?
  • Is there a way of getting an instance of RuntimeTypeModel that behaves the way I would like?
  • Why are the other types in my test not considered to be serializeable the same way TypeB is?
@mgravell
Copy link
Member

mgravell commented Aug 18, 2020 via email

@alennartz
Copy link
Author

Ah Interesting,
It would be nice to be able to opt out of it at some point but no rush. For now I ended up working around the problem by double checking the presence of attributes on top of the type in question. It'l slow down my serializer a bit but i'm caching the result so probably won't be that big of a deal.

Just out of curiosity where in the solution is the code that "makes this work"?

Thanks,

@mgravell
Copy link
Member

mgravell commented Aug 19, 2020 via email

@alennartz
Copy link
Author

Yeah I'm on version 3. It's all pretty much greenfield stuff so i'm pretty flexible at this stage.

@danstur
Copy link

danstur commented Mar 2, 2021

@mgravell What is the state of the issue? Has there been any progress or is this up for grabs?

Would you accept a Pull Request that "fixes" this issue for v3 by providing an option to disable this? (Also if we're at it, any naming preferences for the option?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants