forked from loafle/openapi-generator-original
[c#][netcore] Skip readonly properties in serialization (#7883)
* skip readonly in serialization (c# netcore) * minor wording change
This commit is contained in:
parent
01d0b5d478
commit
b5473d2fda
@ -35,6 +35,17 @@
|
|||||||
{{/description}}
|
{{/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}})]
|
[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; }
|
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}}
|
{{/isEnum}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
{{#hasRequired}}
|
{{#hasRequired}}
|
||||||
@ -118,6 +129,17 @@
|
|||||||
{{/isDate}}
|
{{/isDate}}
|
||||||
public {{{dataType}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; }
|
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}}
|
{{/isEnum}}
|
||||||
{{/isInherited}}
|
{{/isInherited}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
@ -47,12 +47,30 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
||||||
public string Bar { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Foo
|
/// Gets or Sets Foo
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DataMember(Name = "foo", EmitDefaultValue = false)]
|
[DataMember(Name = "foo", EmitDefaultValue = false)]
|
||||||
public string Foo { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets additional properties
|
/// Gets or Sets additional properties
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -64,6 +64,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
|
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
|
||||||
public int SnakeCase { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Property
|
/// Gets or Sets Property
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -76,6 +85,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "123Number", EmitDefaultValue = false)]
|
[DataMember(Name = "123Number", EmitDefaultValue = false)]
|
||||||
public int _123Number { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets additional properties
|
/// Gets or Sets additional properties
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -48,6 +48,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
||||||
public string Bar { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Baz
|
/// Gets or Sets Baz
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -27,7 +27,7 @@ namespace Org.OpenAPITools.Test
|
|||||||
/// Test GetServerUrl
|
/// Test GetServerUrl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void testOneOfSchemaAdditionalProperties()
|
public void TestOneOfSchemaAdditionalProperties()
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
@ -36,7 +36,7 @@ namespace Org.OpenAPITools.Test
|
|||||||
/// Test GetServerUrl
|
/// Test GetServerUrl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void testOneOfSchemaWithDiscriminator()
|
public void TestOneOfSchemaWithDiscriminator()
|
||||||
{
|
{
|
||||||
// Mammal can be one of whale, pig and zebra.
|
// Mammal can be one of whale, pig and zebra.
|
||||||
// pig has sub-classes.
|
// pig has sub-classes.
|
||||||
@ -45,7 +45,7 @@ namespace Org.OpenAPITools.Test
|
|||||||
Mammal m = Mammal.FromJson(str);
|
Mammal m = Mammal.FromJson(str);
|
||||||
Assert.NotNull(m);
|
Assert.NotNull(m);
|
||||||
Assert.IsType<Whale>(m.ActualInstance);
|
Assert.IsType<Whale>(m.ActualInstance);
|
||||||
|
|
||||||
String str2 = "{ \"className\": \"zebra\", \"type\": \"plains\" }";
|
String str2 = "{ \"className\": \"zebra\", \"type\": \"plains\" }";
|
||||||
Mammal m2 = Mammal.FromJson(str2);
|
Mammal m2 = Mammal.FromJson(str2);
|
||||||
Assert.NotNull(m2);
|
Assert.NotNull(m2);
|
||||||
@ -56,7 +56,7 @@ namespace Org.OpenAPITools.Test
|
|||||||
/// Test Fruit
|
/// Test Fruit
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void testFruit()
|
public void TestFruit()
|
||||||
{
|
{
|
||||||
Apple a = new Apple();
|
Apple a = new Apple();
|
||||||
a.Origin = "Japan";
|
a.Origin = "Japan";
|
||||||
@ -95,5 +95,16 @@ namespace Org.OpenAPITools.Test
|
|||||||
// test custom serializer
|
// test custom serializer
|
||||||
Assert.Equal("{\"lengthCm\":98.0}", JsonConvert.SerializeObject(f5));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -47,12 +47,30 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
||||||
public string Bar { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Foo
|
/// Gets or Sets Foo
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DataMember(Name = "foo", EmitDefaultValue = false)]
|
[DataMember(Name = "foo", EmitDefaultValue = false)]
|
||||||
public string Foo { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets additional properties
|
/// Gets or Sets additional properties
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -64,6 +64,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
|
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
|
||||||
public int SnakeCase { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Property
|
/// Gets or Sets Property
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -76,6 +85,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "123Number", EmitDefaultValue = false)]
|
[DataMember(Name = "123Number", EmitDefaultValue = false)]
|
||||||
public int _123Number { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets additional properties
|
/// Gets or Sets additional properties
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -48,6 +48,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
||||||
public string Bar { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Baz
|
/// Gets or Sets Baz
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -46,12 +46,30 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
||||||
public string Bar { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Foo
|
/// Gets or Sets Foo
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DataMember(Name = "foo", EmitDefaultValue = false)]
|
[DataMember(Name = "foo", EmitDefaultValue = false)]
|
||||||
public string Foo { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Returns the string presentation of the object
|
/// Returns the string presentation of the object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -60,6 +60,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
|
[DataMember(Name = "snake_case", EmitDefaultValue = false)]
|
||||||
public int SnakeCase { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Property
|
/// Gets or Sets Property
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -72,6 +81,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "123Number", EmitDefaultValue = false)]
|
[DataMember(Name = "123Number", EmitDefaultValue = false)]
|
||||||
public int _123Number { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Returns the string presentation of the object
|
/// Returns the string presentation of the object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -47,6 +47,15 @@ namespace Org.OpenAPITools.Model
|
|||||||
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
[DataMember(Name = "bar", EmitDefaultValue = false)]
|
||||||
public string Bar { get; private set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or Sets Baz
|
/// Gets or Sets Baz
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user