[c#][netcore] Skip readonly properties in serialization (#7883)

* skip readonly in serialization (c# netcore)

* minor wording change
This commit is contained in:
William Cheng 2020-11-06 10:15:11 +08:00 committed by GitHub
parent 01d0b5d478
commit b5473d2fda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 172 additions and 4 deletions

View File

@ -35,6 +35,17 @@
{{/description}}
[DataMember(Name = "{{baseName}}"{{#required}}, IsRequired = true{{/required}}, EmitDefaultValue = {{#vendorExtensions.x-emit-default-value}}true{{/vendorExtensions.x-emit-default-value}}{{^vendorExtensions.x-emit-default-value}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/vendorExtensions.x-emit-default-value}})]
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; set; }
{{#isReadOnly}}
/// <summary>
/// Returns false as {{name}} should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerialize{{name}}()
{
return false;
}
{{/isReadOnly}}
{{/isEnum}}
{{/vars}}
{{#hasRequired}}
@ -118,6 +129,17 @@
{{/isDate}}
public {{{dataType}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
{{#isReadOnly}}
/// <summary>
/// Returns false as {{name}} should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerialize{{name}}()
{
return false;
}
{{/isReadOnly}}
{{/isEnum}}
{{/isInherited}}
{{/vars}}

View File

@ -47,12 +47,30 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "bar", EmitDefaultValue = false)]
public string Bar { get; private set; }
/// <summary>
/// Returns false as Bar should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeBar()
{
return false;
}
/// <summary>
/// Gets or Sets Foo
/// </summary>
[DataMember(Name = "foo", EmitDefaultValue = false)]
public string Foo { get; private set; }
/// <summary>
/// Returns false as Foo should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeFoo()
{
return false;
}
/// <summary>
/// Gets or Sets additional properties
/// </summary>

View File

@ -64,6 +64,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
public int SnakeCase { get; private set; }
/// <summary>
/// Returns false as SnakeCase should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeSnakeCase()
{
return false;
}
/// <summary>
/// Gets or Sets Property
/// </summary>
@ -76,6 +85,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "123Number", EmitDefaultValue = false)]
public int _123Number { get; private set; }
/// <summary>
/// Returns false as _123Number should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerialize_123Number()
{
return false;
}
/// <summary>
/// Gets or Sets additional properties
/// </summary>

View File

@ -48,6 +48,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "bar", EmitDefaultValue = false)]
public string Bar { get; private set; }
/// <summary>
/// Returns false as Bar should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeBar()
{
return false;
}
/// <summary>
/// Gets or Sets Baz
/// </summary>

View File

@ -27,7 +27,7 @@ namespace Org.OpenAPITools.Test
/// Test GetServerUrl
/// </summary>
[Fact]
public void testOneOfSchemaAdditionalProperties()
public void TestOneOfSchemaAdditionalProperties()
{
// TODO
}
@ -36,7 +36,7 @@ namespace Org.OpenAPITools.Test
/// Test GetServerUrl
/// </summary>
[Fact]
public void testOneOfSchemaWithDiscriminator()
public void TestOneOfSchemaWithDiscriminator()
{
// Mammal can be one of whale, pig and zebra.
// pig has sub-classes.
@ -45,7 +45,7 @@ namespace Org.OpenAPITools.Test
Mammal m = Mammal.FromJson(str);
Assert.NotNull(m);
Assert.IsType<Whale>(m.ActualInstance);
String str2 = "{ \"className\": \"zebra\", \"type\": \"plains\" }";
Mammal m2 = Mammal.FromJson(str2);
Assert.NotNull(m2);
@ -56,7 +56,7 @@ namespace Org.OpenAPITools.Test
/// Test Fruit
/// </summary>
[Fact]
public void testFruit()
public void TestFruit()
{
Apple a = new Apple();
a.Origin = "Japan";
@ -95,5 +95,16 @@ namespace Org.OpenAPITools.Test
// test custom serializer
Assert.Equal("{\"lengthCm\":98.0}", JsonConvert.SerializeObject(f5));
}
/// <summary>
/// ReadOnly property tests
/// </summary>
[Fact]
public void ReadOnlyFruit()
{
ReadOnlyFirst r = JsonConvert.DeserializeObject<ReadOnlyFirst>("{\"baz\":\"from json gaz\",\"bar\":\"from json bar\"}");
Assert.Equal("from json bar", r.Bar);
Assert.Equal("{\"baz\":\"from json gaz\"}", JsonConvert.SerializeObject(r));
}
}
}

View File

@ -47,12 +47,30 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "bar", EmitDefaultValue = false)]
public string Bar { get; private set; }
/// <summary>
/// Returns false as Bar should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeBar()
{
return false;
}
/// <summary>
/// Gets or Sets Foo
/// </summary>
[DataMember(Name = "foo", EmitDefaultValue = false)]
public string Foo { get; private set; }
/// <summary>
/// Returns false as Foo should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeFoo()
{
return false;
}
/// <summary>
/// Gets or Sets additional properties
/// </summary>

View File

@ -64,6 +64,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
public int SnakeCase { get; private set; }
/// <summary>
/// Returns false as SnakeCase should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeSnakeCase()
{
return false;
}
/// <summary>
/// Gets or Sets Property
/// </summary>
@ -76,6 +85,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "123Number", EmitDefaultValue = false)]
public int _123Number { get; private set; }
/// <summary>
/// Returns false as _123Number should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerialize_123Number()
{
return false;
}
/// <summary>
/// Gets or Sets additional properties
/// </summary>

View File

@ -48,6 +48,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "bar", EmitDefaultValue = false)]
public string Bar { get; private set; }
/// <summary>
/// Returns false as Bar should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeBar()
{
return false;
}
/// <summary>
/// Gets or Sets Baz
/// </summary>

View File

@ -46,12 +46,30 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "bar", EmitDefaultValue = false)]
public string Bar { get; private set; }
/// <summary>
/// Returns false as Bar should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeBar()
{
return false;
}
/// <summary>
/// Gets or Sets Foo
/// </summary>
[DataMember(Name = "foo", EmitDefaultValue = false)]
public string Foo { get; private set; }
/// <summary>
/// Returns false as Foo should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeFoo()
{
return false;
}
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@ -60,6 +60,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
public int SnakeCase { get; private set; }
/// <summary>
/// Returns false as SnakeCase should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeSnakeCase()
{
return false;
}
/// <summary>
/// Gets or Sets Property
/// </summary>
@ -72,6 +81,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "123Number", EmitDefaultValue = false)]
public int _123Number { get; private set; }
/// <summary>
/// Returns false as _123Number should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerialize_123Number()
{
return false;
}
/// <summary>
/// Returns the string presentation of the object
/// </summary>

View File

@ -47,6 +47,15 @@ namespace Org.OpenAPITools.Model
[DataMember(Name = "bar", EmitDefaultValue = false)]
public string Bar { get; private set; }
/// <summary>
/// Returns false as Bar should not be serialized given that it's read-only.
/// </summary>
/// <returns>false (boolean)</returns>
public bool ShouldSerializeBar()
{
return false;
}
/// <summary>
/// Gets or Sets Baz
/// </summary>