Skip to content

Commit b5473d2

Browse files
authored
[c#][netcore] Skip readonly properties in serialization (#7883)
* skip readonly in serialization (c# netcore) * minor wording change
1 parent 01d0b5d commit b5473d2

File tree

11 files changed

+172
-4
lines changed

11 files changed

+172
-4
lines changed

modules/openapi-generator/src/main/resources/csharp-netcore/modelGeneric.mustache

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
{{/description}}
3636
[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}})]
3737
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; set; }
38+
{{#isReadOnly}}
39+
40+
/// <summary>
41+
/// Returns false as {{name}} should not be serialized given that it's read-only.
42+
/// </summary>
43+
/// <returns>false (boolean)</returns>
44+
public bool ShouldSerialize{{name}}()
45+
{
46+
return false;
47+
}
48+
{{/isReadOnly}}
3849
{{/isEnum}}
3950
{{/vars}}
4051
{{#hasRequired}}
@@ -118,6 +129,17 @@
118129
{{/isDate}}
119130
public {{{dataType}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
120131

132+
{{#isReadOnly}}
133+
/// <summary>
134+
/// Returns false as {{name}} should not be serialized given that it's read-only.
135+
/// </summary>
136+
/// <returns>false (boolean)</returns>
137+
public bool ShouldSerialize{{name}}()
138+
{
139+
return false;
140+
}
141+
142+
{{/isReadOnly}}
121143
{{/isEnum}}
122144
{{/isInherited}}
123145
{{/vars}}

samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Model/HasOnlyReadOnly.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,30 @@ public HasOnlyReadOnly()
4747
[DataMember(Name = "bar", EmitDefaultValue = false)]
4848
public string Bar { get; private set; }
4949

50+
/// <summary>
51+
/// Returns false as Bar should not be serialized given that it's read-only.
52+
/// </summary>
53+
/// <returns>false (boolean)</returns>
54+
public bool ShouldSerializeBar()
55+
{
56+
return false;
57+
}
58+
5059
/// <summary>
5160
/// Gets or Sets Foo
5261
/// </summary>
5362
[DataMember(Name = "foo", EmitDefaultValue = false)]
5463
public string Foo { get; private set; }
5564

65+
/// <summary>
66+
/// Returns false as Foo should not be serialized given that it's read-only.
67+
/// </summary>
68+
/// <returns>false (boolean)</returns>
69+
public bool ShouldSerializeFoo()
70+
{
71+
return false;
72+
}
73+
5674
/// <summary>
5775
/// Gets or Sets additional properties
5876
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Model/Name.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ protected Name()
6464
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
6565
public int SnakeCase { get; private set; }
6666

67+
/// <summary>
68+
/// Returns false as SnakeCase should not be serialized given that it's read-only.
69+
/// </summary>
70+
/// <returns>false (boolean)</returns>
71+
public bool ShouldSerializeSnakeCase()
72+
{
73+
return false;
74+
}
75+
6776
/// <summary>
6877
/// Gets or Sets Property
6978
/// </summary>
@@ -76,6 +85,15 @@ protected Name()
7685
[DataMember(Name = "123Number", EmitDefaultValue = false)]
7786
public int _123Number { get; private set; }
7887

88+
/// <summary>
89+
/// Returns false as _123Number should not be serialized given that it's read-only.
90+
/// </summary>
91+
/// <returns>false (boolean)</returns>
92+
public bool ShouldSerialize_123Number()
93+
{
94+
return false;
95+
}
96+
7997
/// <summary>
8098
/// Gets or Sets additional properties
8199
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Model/ReadOnlyFirst.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public partial class ReadOnlyFirst : IEquatable<ReadOnlyFirst>, IValidatableObje
4848
[DataMember(Name = "bar", EmitDefaultValue = false)]
4949
public string Bar { get; private set; }
5050

51+
/// <summary>
52+
/// Returns false as Bar should not be serialized given that it's read-only.
53+
/// </summary>
54+
/// <returns>false (boolean)</returns>
55+
public bool ShouldSerializeBar()
56+
{
57+
return false;
58+
}
59+
5160
/// <summary>
5261
/// Gets or Sets Baz
5362
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/JSONComposedSchemaTests.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public JSONComposedSchemaTests()
2727
/// Test GetServerUrl
2828
/// </summary>
2929
[Fact]
30-
public void testOneOfSchemaAdditionalProperties()
30+
public void TestOneOfSchemaAdditionalProperties()
3131
{
3232
// TODO
3333
}
@@ -36,7 +36,7 @@ public void testOneOfSchemaAdditionalProperties()
3636
/// Test GetServerUrl
3737
/// </summary>
3838
[Fact]
39-
public void testOneOfSchemaWithDiscriminator()
39+
public void TestOneOfSchemaWithDiscriminator()
4040
{
4141
// Mammal can be one of whale, pig and zebra.
4242
// pig has sub-classes.
@@ -45,7 +45,7 @@ public void testOneOfSchemaWithDiscriminator()
4545
Mammal m = Mammal.FromJson(str);
4646
Assert.NotNull(m);
4747
Assert.IsType<Whale>(m.ActualInstance);
48-
48+
4949
String str2 = "{ \"className\": \"zebra\", \"type\": \"plains\" }";
5050
Mammal m2 = Mammal.FromJson(str2);
5151
Assert.NotNull(m2);
@@ -56,7 +56,7 @@ public void testOneOfSchemaWithDiscriminator()
5656
/// Test Fruit
5757
/// </summary>
5858
[Fact]
59-
public void testFruit()
59+
public void TestFruit()
6060
{
6161
Apple a = new Apple();
6262
a.Origin = "Japan";
@@ -95,5 +95,16 @@ public void testFruit()
9595
// test custom serializer
9696
Assert.Equal("{\"lengthCm\":98.0}", JsonConvert.SerializeObject(f5));
9797
}
98+
99+
/// <summary>
100+
/// ReadOnly property tests
101+
/// </summary>
102+
[Fact]
103+
public void ReadOnlyFruit()
104+
{
105+
ReadOnlyFirst r = JsonConvert.DeserializeObject<ReadOnlyFirst>("{\"baz\":\"from json gaz\",\"bar\":\"from json bar\"}");
106+
Assert.Equal("from json bar", r.Bar);
107+
Assert.Equal("{\"baz\":\"from json gaz\"}", JsonConvert.SerializeObject(r));
108+
}
98109
}
99110
}

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/HasOnlyReadOnly.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,30 @@ public HasOnlyReadOnly()
4747
[DataMember(Name = "bar", EmitDefaultValue = false)]
4848
public string Bar { get; private set; }
4949

50+
/// <summary>
51+
/// Returns false as Bar should not be serialized given that it's read-only.
52+
/// </summary>
53+
/// <returns>false (boolean)</returns>
54+
public bool ShouldSerializeBar()
55+
{
56+
return false;
57+
}
58+
5059
/// <summary>
5160
/// Gets or Sets Foo
5261
/// </summary>
5362
[DataMember(Name = "foo", EmitDefaultValue = false)]
5463
public string Foo { get; private set; }
5564

65+
/// <summary>
66+
/// Returns false as Foo should not be serialized given that it's read-only.
67+
/// </summary>
68+
/// <returns>false (boolean)</returns>
69+
public bool ShouldSerializeFoo()
70+
{
71+
return false;
72+
}
73+
5674
/// <summary>
5775
/// Gets or Sets additional properties
5876
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Name.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ protected Name()
6464
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
6565
public int SnakeCase { get; private set; }
6666

67+
/// <summary>
68+
/// Returns false as SnakeCase should not be serialized given that it's read-only.
69+
/// </summary>
70+
/// <returns>false (boolean)</returns>
71+
public bool ShouldSerializeSnakeCase()
72+
{
73+
return false;
74+
}
75+
6776
/// <summary>
6877
/// Gets or Sets Property
6978
/// </summary>
@@ -76,6 +85,15 @@ protected Name()
7685
[DataMember(Name = "123Number", EmitDefaultValue = false)]
7786
public int _123Number { get; private set; }
7887

88+
/// <summary>
89+
/// Returns false as _123Number should not be serialized given that it's read-only.
90+
/// </summary>
91+
/// <returns>false (boolean)</returns>
92+
public bool ShouldSerialize_123Number()
93+
{
94+
return false;
95+
}
96+
7997
/// <summary>
8098
/// Gets or Sets additional properties
8199
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/ReadOnlyFirst.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public partial class ReadOnlyFirst : IEquatable<ReadOnlyFirst>, IValidatableObje
4848
[DataMember(Name = "bar", EmitDefaultValue = false)]
4949
public string Bar { get; private set; }
5050

51+
/// <summary>
52+
/// Returns false as Bar should not be serialized given that it's read-only.
53+
/// </summary>
54+
/// <returns>false (boolean)</returns>
55+
public bool ShouldSerializeBar()
56+
{
57+
return false;
58+
}
59+
5160
/// <summary>
5261
/// Gets or Sets Baz
5362
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/HasOnlyReadOnly.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,30 @@ public HasOnlyReadOnly()
4646
[DataMember(Name = "bar", EmitDefaultValue = false)]
4747
public string Bar { get; private set; }
4848

49+
/// <summary>
50+
/// Returns false as Bar should not be serialized given that it's read-only.
51+
/// </summary>
52+
/// <returns>false (boolean)</returns>
53+
public bool ShouldSerializeBar()
54+
{
55+
return false;
56+
}
57+
4958
/// <summary>
5059
/// Gets or Sets Foo
5160
/// </summary>
5261
[DataMember(Name = "foo", EmitDefaultValue = false)]
5362
public string Foo { get; private set; }
5463

64+
/// <summary>
65+
/// Returns false as Foo should not be serialized given that it's read-only.
66+
/// </summary>
67+
/// <returns>false (boolean)</returns>
68+
public bool ShouldSerializeFoo()
69+
{
70+
return false;
71+
}
72+
5573
/// <summary>
5674
/// Returns the string presentation of the object
5775
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Name.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ protected Name() { }
6060
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
6161
public int SnakeCase { get; private set; }
6262

63+
/// <summary>
64+
/// Returns false as SnakeCase should not be serialized given that it's read-only.
65+
/// </summary>
66+
/// <returns>false (boolean)</returns>
67+
public bool ShouldSerializeSnakeCase()
68+
{
69+
return false;
70+
}
71+
6372
/// <summary>
6473
/// Gets or Sets Property
6574
/// </summary>
@@ -72,6 +81,15 @@ protected Name() { }
7281
[DataMember(Name = "123Number", EmitDefaultValue = false)]
7382
public int _123Number { get; private set; }
7483

84+
/// <summary>
85+
/// Returns false as _123Number should not be serialized given that it's read-only.
86+
/// </summary>
87+
/// <returns>false (boolean)</returns>
88+
public bool ShouldSerialize_123Number()
89+
{
90+
return false;
91+
}
92+
7593
/// <summary>
7694
/// Returns the string presentation of the object
7795
/// </summary>

0 commit comments

Comments
 (0)