diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java index 5e3c1e1cfa9..7a63c7b95ec 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java @@ -588,6 +588,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen { final String testPackageName = testPackageName(); String packageFolder = sourceFolder + File.separator + packageName; String clientPackageDir = packageFolder + File.separator + clientPackage; + String modelPackageDir = packageFolder + File.separator + modelPackage; String testPackageFolder = testFolder + File.separator + testPackageName; additionalProperties.put("testPackageName", testPackageName); @@ -645,6 +646,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen { } supportingFiles.add(new SupportingFile("appveyor.mustache", "", "appveyor.yml")); + supportingFiles.add(new SupportingFile("AbstractOpenAPISchema.mustache", modelPackageDir, "AbstractOpenAPISchema.cs")); additionalProperties.put("apiDocPath", apiDocPath); additionalProperties.put("modelDocPath", modelDocPath); @@ -938,4 +940,31 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen { return null; } } + + @SuppressWarnings("unchecked") + @Override + public Map postProcessModels(Map objs) { + objs = super.postProcessModels(objs); + List models = (List) objs.get("models"); + + // add implements for serializable/parcelable to all models + for (Object _mo : models) { + Map mo = (Map) _mo; + CodegenModel cm = (CodegenModel) mo.get("model"); + + if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.contains("ModelNull")) { + // if oneOf contains "null" type + cm.isNullable = true; + cm.oneOf.remove("ModelNull"); + } + + if (cm.anyOf != null && !cm.anyOf.isEmpty() && cm.anyOf.contains("ModelNull")) { + // if anyOf contains "null" type + cm.isNullable = true; + cm.anyOf.remove("ModelNull"); + } + } + + return objs; + } } diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/AbstractOpenAPISchema.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/AbstractOpenAPISchema.mustache new file mode 100644 index 00000000000..197cd9bed23 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/AbstractOpenAPISchema.mustache @@ -0,0 +1,53 @@ +{{>partial_header}} + +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace {{packageName}}.{{modelPackage}} +{ + /// + /// Abstract base class for oneOf, anyOf schemas in the OpenAPI specification + /// + {{>visibility}} abstract partial class AbstractOpenAPISchema + { + protected readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings + { + // OpenAPI generated types generally hide default constructors. + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, + MissingMemberHandling = MissingMemberHandling.Error, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy + { + OverrideSpecifiedNames = false + } + } + }; + + /// + /// Gets or Sets the actual instance + /// + public abstract Object ActualInstance { get; set; } + + /// + /// Gets or Sets IsNullable to indicate whether the instance is nullable + /// + public bool IsNullable { get; protected set; } + + /// + /// Gets or Sets the schema type, which can be either `oneOf` or `anyOf` + /// + public string SchemaType { get; protected set; } + + /// + /// Converts the instance into JSON string. + /// + public abstract string ToJson(); + + /// + /// Converts the JSON string into the instance + /// + public abstract void FromJson(string jsonString); + } +} diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache index 92503de2a7e..9da7ef4cb49 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache @@ -5,17 +5,18 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; -{{^netStandard}} -using System.Web; -{{/netStandard}} using System.Linq; using System.Net; +using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Text; using System.Threading; using System.Text.RegularExpressions; using System.Threading.Tasks; +{{^netStandard}} +using System.Web; +{{/netStandard}} using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using RestSharp; @@ -59,8 +60,15 @@ namespace {{packageName}}.Client public string Serialize(object obj) { - var result = JsonConvert.SerializeObject(obj, _serializerSettings); - return result; + if (obj != null && obj.GetType().IsInstanceOfType(typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema))) + { + // the object to be serialized is an oneOf/anyOf schema + return ((Org.OpenAPITools.Model.AbstractOpenAPISchema)obj).ToJson(); + } + else + { + return JsonConvert.SerializeObject(obj, _serializerSettings); + } } public T Deserialize(IRestResponse response) @@ -120,7 +128,17 @@ namespace {{packageName}}.Client // at this point, it must be a model (json) try { - return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings); + if (type.IsInstanceOfType(typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema))) + { + // the response is an oneOf/anyOf schema + Org.OpenAPITools.Model.AbstractOpenAPISchema instance = (Org.OpenAPITools.Model.AbstractOpenAPISchema)Activator.CreateInstance(type); + instance.FromJson(response.Content); + return instance; + } + else + { + return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings); + } } catch (Exception e) { @@ -437,6 +455,15 @@ namespace {{packageName}}.Client response = client.Execute(req); } + // if the response type is oneOf/anyOf, call FromJSON to deserialize the data + if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) + { + T instance = (T)Activator.CreateInstance(typeof(T)); + MethodInfo method = typeof(T).GetMethod("FromJson"); + method.Invoke(instance, new object[] { response.Content }); + response.Data = instance; + } + InterceptResponse(req, response); var result = ToApiResponse(response); @@ -535,6 +562,15 @@ namespace {{packageName}}.Client response = await client.ExecuteAsync(req, cancellationToken).ConfigureAwait(false); } + // if the response type is oneOf/anyOf, call FromJSON to deserialize the data + if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) + { + T instance = (T)Activator.CreateInstance(typeof(T)); + MethodInfo method = typeof(T).GetMethod("FromJson"); + method.Invoke(instance, new object[] { response.Content }); + response.Data = instance; + } + InterceptResponse(req, response); var result = ToApiResponse(response); diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache index 26ce826e030..393bdd01b03 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache @@ -30,7 +30,7 @@ using OpenAPIClientUtils = {{packageName}}.Client.ClientUtils; namespace {{packageName}}.{{modelPackage}} { -{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelGeneric}}{{/isEnum}} +{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}} {{/model}} {{/models}} } diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/modelOneOf.mustache new file mode 100644 index 00000000000..a745159b85d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/modelOneOf.mustache @@ -0,0 +1,184 @@ + /// + /// {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}} + /// + [DataContract(Name = "{{{name}}}")] + {{>visibility}} partial class {{classname}} : AbstractOpenAPISchema, {{#parent}}{{{parent}}}, {{/parent}}IEquatable<{{classname}}>{{#validatable}}, IValidatableObject{{/validatable}} + { + /// + /// Initializes a new instance of the class. + /// + public {{classname}}() + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + } + + {{#oneOf}} + /// + /// Initializes a new instance of the class + /// with the class + /// + /// An instance of {{{.}}}. + public {{classname}}({{{.}}} actualInstance) + { + this.IsNullable = {{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance{{^isNullable}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isNullable}}; + } + + {{/oneOf}} + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + {{#oneOf}} + {{^-first}}else {{/-first}}if (value.GetType() == typeof({{{.}}})) + { + this._actualInstance = value; + } + {{/oneOf}} + else + { + throw new ArgumentException("Invalid instance found. Must be the following types:{{#oneOf}} {{{.}}}{{^-last}},{{/-last}}{{/oneOf}}"); + } + } + } + {{#oneOf}} + + /// + /// Get the actual instance of `{{{.}}}`. If the actual instanct is not `{{{.}}}`, + /// the InvalidClassException will be thrown + /// + /// An instance of {{{.}}} + public {{{.}}} Get{{{.}}}() + { + return ({{{.}}})this.ActualInstance; + } + {{/oneOf}} + + /// + /// Returns the string presentation of the object + /// + /// String presentation of the object + public override string ToString() + { + var sb = new StringBuilder(); + sb.Append("class {{classname}} {\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); + sb.Append("}\n"); + return sb.ToString(); + } + + /// + /// Returns the JSON string presentation of the object + /// + /// JSON string presentation of the object + public override string ToJson() + { + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + {{#oneOf}} + + try + { + this.ActualInstance = JsonConvert.DeserializeObject<{{{.}}}>(jsonString, _serializerSettings); + matchedTypes.Add("{{{.}}}"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + {{/oneOf}} + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. + } + + /// + /// Returns true if objects are equal + /// + /// Object to be compared + /// Boolean + public override bool Equals(object input) + { + {{#useCompareNetObjects}} + return OpenAPIClientUtils.compareLogic.Compare(this, input as {{classname}}).AreEqual; + {{/useCompareNetObjects}} + {{^useCompareNetObjects}} + return this.Equals(input as {{classname}}); + {{/useCompareNetObjects}} + } + + /// + /// Returns true if {{classname}} instances are equal + /// + /// Instance of {{classname}} to be compared + /// Boolean + public bool Equals({{classname}} input) + { + {{#useCompareNetObjects}} + return OpenAPIClientUtils.compareLogic.Compare(this, input).AreEqual; + {{/useCompareNetObjects}} + {{^useCompareNetObjects}} + if (input == null) + return false; + + return this.ActualInstance.Equals(input.ActualInstance); + {{/useCompareNetObjects}} + } + + /// + /// Gets the hash code + /// + /// Hash code + public override int GetHashCode() + { + unchecked // Overflow is fine, just wrap + { + int hashCode = 41; + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); + return hashCode; + } + } + + /// + /// To validate all properties of the instance + /// + /// Validation context + /// Validation Result + IEnumerable IValidatableObject.Validate(ValidationContext validationContext) + { + yield break; + } + } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/.openapi-generator/FILES b/samples/client/petstore/csharp-netcore/OpenAPIClient/.openapi-generator/FILES index 28a46aca90c..4894085ce8f 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/.openapi-generator/FILES +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/.openapi-generator/FILES @@ -114,6 +114,7 @@ src/Org.OpenAPITools/Client/Multimap.cs src/Org.OpenAPITools/Client/OpenAPIDateConverter.cs src/Org.OpenAPITools/Client/RequestOptions.cs src/Org.OpenAPITools/Client/RetryConfiguration.cs +src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs src/Org.OpenAPITools/Model/AdditionalPropertiesClass.cs src/Org.OpenAPITools/Model/Animal.cs src/Org.OpenAPITools/Model/ApiResponse.cs diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/JSONComposedSchemaTests.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/JSONComposedSchemaTests.cs new file mode 100644 index 00000000000..c71ebfcb185 --- /dev/null +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/JSONComposedSchemaTests.cs @@ -0,0 +1,71 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Reflection; +using RestSharp; +using Xunit; + +using Org.OpenAPITools.Client; +using Org.OpenAPITools.Api; +using Org.OpenAPITools.Model; + +namespace Org.OpenAPITools.Test +{ + /// + /// Class for testing Configuration + /// + public class JSONComposedSchemaTests + { + public JSONComposedSchemaTests() + { + } + + /// + /// Test GetServerUrl + /// + [Fact] + public void testOneOfSchemaAdditionalProperties() + { + // TODO + } + + /// + /// Test Fruit + /// + [Fact] + public void testFruit() + { + Apple a = new Apple(); + a.Origin = "Japan"; + + Banana b = new Banana(); + b.LengthCm = 13; + + Tag t = new Tag(); + t.Id = 12; + t.Name = "Something"; + + Fruit f1 = new Fruit(a); + Fruit f2 = new Fruit(a); + + f1.ActualInstance = b; + f2.ActualInstance = a; + + Assert.Equal(13, f1.GetBanana().LengthCm); + Assert.Equal("Japan", f2.GetApple().Origin); + + Assert.Throws(() => f1.ActualInstance = t); + + Assert.Equal("{\"lengthCm\":13.0}", f1.ToJson()); + Assert.Equal("{\"origin\":\"Japan\"}", f2.ToJson()); + + f1.FromJson("{\"lengthCm\":98}"); + Assert.IsType(f1.ActualInstance); + + f2.FromJson("{\"origin\":\"Japan\"}"); + Assert.IsType(f2.ActualInstance); + } + } +} \ No newline at end of file diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs index d9f7099014d..c76989de695 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -15,6 +15,7 @@ using System.Globalization; using System.IO; using System.Linq; using System.Net; +using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Text; @@ -64,8 +65,15 @@ namespace Org.OpenAPITools.Client public string Serialize(object obj) { - var result = JsonConvert.SerializeObject(obj, _serializerSettings); - return result; + if (obj != null && obj.GetType().IsInstanceOfType(typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema))) + { + // the object to be serialized is an oneOf/anyOf schema + return ((Org.OpenAPITools.Model.AbstractOpenAPISchema)obj).ToJson(); + } + else + { + return JsonConvert.SerializeObject(obj, _serializerSettings); + } } public T Deserialize(IRestResponse response) @@ -125,7 +133,17 @@ namespace Org.OpenAPITools.Client // at this point, it must be a model (json) try { - return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings); + if (type.IsInstanceOfType(typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema))) + { + // the response is an oneOf/anyOf schema + Org.OpenAPITools.Model.AbstractOpenAPISchema instance = (Org.OpenAPITools.Model.AbstractOpenAPISchema)Activator.CreateInstance(type); + instance.FromJson(response.Content); + return instance; + } + else + { + return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings); + } } catch (Exception e) { @@ -441,6 +459,15 @@ namespace Org.OpenAPITools.Client response = client.Execute(req); } + // if the response type is oneOf/anyOf, call FromJSON to deserialize the data + if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) + { + T instance = (T)Activator.CreateInstance(typeof(T)); + MethodInfo method = typeof(T).GetMethod("FromJson"); + method.Invoke(instance, new object[] { response.Content }); + response.Data = instance; + } + InterceptResponse(req, response); var result = ToApiResponse(response); @@ -538,6 +565,15 @@ namespace Org.OpenAPITools.Client response = await client.ExecuteAsync(req, cancellationToken).ConfigureAwait(false); } + // if the response type is oneOf/anyOf, call FromJSON to deserialize the data + if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) + { + T instance = (T)Activator.CreateInstance(typeof(T)); + MethodInfo method = typeof(T).GetMethod("FromJson"); + method.Invoke(instance, new object[] { response.Content }); + response.Data = instance; + } + InterceptResponse(req, response); var result = ToApiResponse(response); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs new file mode 100644 index 00000000000..718110f9ac7 --- /dev/null +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs @@ -0,0 +1,61 @@ +/* + * OpenAPI Petstore + * + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * The version of the OpenAPI document: 1.0.0 + * Generated by: https://github.com/openapitools/openapi-generator.git + */ + + +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace Org.OpenAPITools.Model +{ + /// + /// Abstract base class for oneOf, anyOf schemas in the OpenAPI specification + /// + public abstract partial class AbstractOpenAPISchema + { + protected readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings + { + // OpenAPI generated types generally hide default constructors. + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, + MissingMemberHandling = MissingMemberHandling.Error, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy + { + OverrideSpecifiedNames = false + } + } + }; + + /// + /// Gets or Sets the actual instance + /// + public abstract Object ActualInstance { get; set; } + + /// + /// Gets or Sets IsNullable to indicate whether the instance is nullable + /// + public bool IsNullable { get; protected set; } + + /// + /// Gets or Sets the schema type, which can be either `oneOf` or `anyOf` + /// + public string SchemaType { get; protected set; } + + /// + /// Converts the instance into JSON string. + /// + public abstract string ToJson(); + + /// + /// Converts the JSON string into the instance + /// + public abstract void FromJson(string jsonString); + } +} diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Fruit.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Fruit.cs index 7dcf395dbad..e8cf40b9d52 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Fruit.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Fruit.cs @@ -29,46 +29,89 @@ namespace Org.OpenAPITools.Model /// Fruit /// [DataContract(Name = "fruit")] - public partial class Fruit : IEquatable, IValidatableObject + public partial class Fruit : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - /// color. - /// cultivar. - /// origin. - /// lengthCm. - public Fruit(string color = default(string), string cultivar = default(string), string origin = default(string), decimal lengthCm = default(decimal)) + public Fruit() { - this.Color = color; - this.Cultivar = cultivar; - this.Origin = origin; - this.LengthCm = lengthCm; + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets Color + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "color", EmitDefaultValue = false)] - public string Color { get; set; } + /// An instance of Apple. + public Fruit(Apple actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets Cultivar + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "cultivar", EmitDefaultValue = false)] - public string Cultivar { get; set; } + /// An instance of Banana. + public Fruit(Banana actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets Origin + /// Gets or Sets ActualInstance /// - [DataMember(Name = "origin", EmitDefaultValue = false)] - public string Origin { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Apple)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Banana)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Apple, Banana"); + } + } + } /// - /// Gets or Sets LengthCm + /// Get the actual instance of `Apple`. If the actual instanct is not `Apple`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "lengthCm", EmitDefaultValue = false)] - public decimal LengthCm { get; set; } + /// An instance of Apple + public Apple GetApple() + { + return (Apple)this.ActualInstance; + } + + /// + /// Get the actual instance of `Banana`. If the actual instanct is not `Banana`, + /// the InvalidClassException will be thrown + /// + /// An instance of Banana + public Banana GetBanana() + { + return (Banana)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -78,10 +121,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Fruit {\n"); - sb.Append(" Color: ").Append(Color).Append("\n"); - sb.Append(" Cultivar: ").Append(Cultivar).Append("\n"); - sb.Append(" Origin: ").Append(Origin).Append("\n"); - sb.Append(" LengthCm: ").Append(LengthCm).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -90,9 +130,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Apple"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Banana"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -124,13 +211,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.Color != null) - hashCode = hashCode * 59 + this.Color.GetHashCode(); - if (this.Cultivar != null) - hashCode = hashCode * 59 + this.Cultivar.GetHashCode(); - if (this.Origin != null) - hashCode = hashCode * 59 + this.Origin.GetHashCode(); - hashCode = hashCode * 59 + this.LengthCm.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -142,20 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) { - // Cultivar (string) pattern - Regex regexCultivar = new Regex(@"^[a-zA-Z\\s]*$", RegexOptions.CultureInvariant); - if (false == regexCultivar.Match(this.Cultivar).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Cultivar, must match a pattern of " + regexCultivar, new [] { "Cultivar" }); - } - - // Origin (string) pattern - Regex regexOrigin = new Regex(@"^[A-Z\\s]*$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); - if (false == regexOrigin.Match(this.Origin).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Origin, must match a pattern of " + regexOrigin, new [] { "Origin" }); - } - yield break; } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/FruitReq.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/FruitReq.cs index 9d0eafa09b7..c1604f4ae86 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/FruitReq.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/FruitReq.cs @@ -29,52 +29,89 @@ namespace Org.OpenAPITools.Model /// FruitReq /// [DataContract(Name = "fruitReq")] - public partial class FruitReq : IEquatable, IValidatableObject + public partial class FruitReq : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected FruitReq() { } - /// - /// Initializes a new instance of the class. - /// - /// cultivar (required). - /// mealy. - /// lengthCm (required). - /// sweet. - public FruitReq(string cultivar = default(string), bool mealy = default(bool), decimal lengthCm = default(decimal), bool sweet = default(bool)) + public FruitReq() { - // to ensure "cultivar" is required (not null) - this.Cultivar = cultivar ?? throw new ArgumentNullException("cultivar is a required property for FruitReq and cannot be null"); - this.LengthCm = lengthCm; - this.Mealy = mealy; - this.Sweet = sweet; + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets Cultivar + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "cultivar", IsRequired = true, EmitDefaultValue = false)] - public string Cultivar { get; set; } + /// An instance of AppleReq. + public FruitReq(AppleReq actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } /// - /// Gets or Sets Mealy + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "mealy", EmitDefaultValue = false)] - public bool Mealy { get; set; } + /// An instance of BananaReq. + public FruitReq(BananaReq actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } + + + private Object _actualInstance; /// - /// Gets or Sets LengthCm + /// Gets or Sets ActualInstance /// - [DataMember(Name = "lengthCm", IsRequired = true, EmitDefaultValue = false)] - public decimal LengthCm { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(AppleReq)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(BananaReq)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: AppleReq, BananaReq"); + } + } + } /// - /// Gets or Sets Sweet + /// Get the actual instance of `AppleReq`. If the actual instanct is not `AppleReq`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "sweet", EmitDefaultValue = false)] - public bool Sweet { get; set; } + /// An instance of AppleReq + public AppleReq GetAppleReq() + { + return (AppleReq)this.ActualInstance; + } + + /// + /// Get the actual instance of `BananaReq`. If the actual instanct is not `BananaReq`, + /// the InvalidClassException will be thrown + /// + /// An instance of BananaReq + public BananaReq GetBananaReq() + { + return (BananaReq)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -84,10 +121,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class FruitReq {\n"); - sb.Append(" Cultivar: ").Append(Cultivar).Append("\n"); - sb.Append(" Mealy: ").Append(Mealy).Append("\n"); - sb.Append(" LengthCm: ").Append(LengthCm).Append("\n"); - sb.Append(" Sweet: ").Append(Sweet).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -96,9 +130,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("AppleReq"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("BananaReq"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -130,11 +211,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.Cultivar != null) - hashCode = hashCode * 59 + this.Cultivar.GetHashCode(); - hashCode = hashCode * 59 + this.Mealy.GetHashCode(); - hashCode = hashCode * 59 + this.LengthCm.GetHashCode(); - hashCode = hashCode * 59 + this.Sweet.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Mammal.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Mammal.cs index de6192270e2..15ac75fcbfb 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Mammal.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Mammal.cs @@ -30,88 +30,115 @@ namespace Org.OpenAPITools.Model /// Mammal /// [DataContract(Name = "mammal")] - [JsonConverter(typeof(JsonSubtypes), "ClassName")] - public partial class Mammal : IEquatable, IValidatableObject + public partial class Mammal : AbstractOpenAPISchema, IEquatable, IValidatableObject { - /// - /// Defines Type - /// - [JsonConverter(typeof(StringEnumConverter))] - public enum TypeEnum - { - /// - /// Enum Plains for value: plains - /// - [EnumMember(Value = "plains")] - Plains = 1, - - /// - /// Enum Mountain for value: mountain - /// - [EnumMember(Value = "mountain")] - Mountain = 2, - - /// - /// Enum Grevys for value: grevys - /// - [EnumMember(Value = "grevys")] - Grevys = 3 - - } - - /// - /// Gets or Sets Type - /// - [DataMember(Name = "type", EmitDefaultValue = false)] - public TypeEnum? Type { get; set; } /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Mammal() + public Mammal() { - this.AdditionalProperties = new Dictionary(); - } - /// - /// Initializes a new instance of the class. - /// - /// hasBaleen. - /// hasTeeth. - /// className (required). - /// type. - public Mammal(bool hasBaleen = default(bool), bool hasTeeth = default(bool), string className = default(string), TypeEnum? type = default(TypeEnum?)) - { - // to ensure "className" is required (not null) - this.ClassName = className ?? throw new ArgumentNullException("className is a required property for Mammal and cannot be null"); - this.HasBaleen = hasBaleen; - this.HasTeeth = hasTeeth; - this.Type = type; - this.AdditionalProperties = new Dictionary(); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets HasBaleen + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "hasBaleen", EmitDefaultValue = false)] - public bool HasBaleen { get; set; } + /// An instance of Pig. + public Mammal(Pig actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets HasTeeth + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "hasTeeth", EmitDefaultValue = false)] - public bool HasTeeth { get; set; } + /// An instance of Whale. + public Mammal(Whale actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets ClassName + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "className", IsRequired = true, EmitDefaultValue = false)] - public string ClassName { get; set; } + /// An instance of Zebra. + public Mammal(Zebra actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets additional properties + /// Gets or Sets ActualInstance /// - [JsonExtensionData] - public IDictionary AdditionalProperties { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Pig)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Whale)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Zebra)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Pig, Whale, Zebra"); + } + } + } + + /// + /// Get the actual instance of `Pig`. If the actual instanct is not `Pig`, + /// the InvalidClassException will be thrown + /// + /// An instance of Pig + public Pig GetPig() + { + return (Pig)this.ActualInstance; + } + + /// + /// Get the actual instance of `Whale`. If the actual instanct is not `Whale`, + /// the InvalidClassException will be thrown + /// + /// An instance of Whale + public Whale GetWhale() + { + return (Whale)this.ActualInstance; + } + + /// + /// Get the actual instance of `Zebra`. If the actual instanct is not `Zebra`, + /// the InvalidClassException will be thrown + /// + /// An instance of Zebra + public Zebra GetZebra() + { + return (Zebra)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -121,11 +148,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Mammal {\n"); - sb.Append(" HasBaleen: ").Append(HasBaleen).Append("\n"); - sb.Append(" HasTeeth: ").Append(HasTeeth).Append("\n"); - sb.Append(" ClassName: ").Append(ClassName).Append("\n"); - sb.Append(" Type: ").Append(Type).Append("\n"); - sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -134,9 +157,69 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Pig"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Whale"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Zebra"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -168,13 +251,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - hashCode = hashCode * 59 + this.HasBaleen.GetHashCode(); - hashCode = hashCode * 59 + this.HasTeeth.GetHashCode(); - if (this.ClassName != null) - hashCode = hashCode * 59 + this.ClassName.GetHashCode(); - hashCode = hashCode * 59 + this.Type.GetHashCode(); - if (this.AdditionalProperties != null) - hashCode = hashCode * 59 + this.AdditionalProperties.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -185,16 +263,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/NullableShape.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/NullableShape.cs index 6a177976e93..03e206f92ad 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/NullableShape.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/NullableShape.cs @@ -30,49 +30,89 @@ namespace Org.OpenAPITools.Model /// The value may be a shape or the 'null' value. The 'nullable' attribute was introduced in OAS schema >= 3.0 and has been deprecated in OAS schema >= 3.1. /// [DataContract(Name = "NullableShape")] - [JsonConverter(typeof(JsonSubtypes), "ShapeType")] - public partial class NullableShape : IEquatable, IValidatableObject + public partial class NullableShape : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected NullableShape() + public NullableShape() { - this.AdditionalProperties = new Dictionary(); - } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - /// triangleType (required). - public NullableShape(string shapeType = default(string), string quadrilateralType = default(string), string triangleType = default(string)) - { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for NullableShape and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for NullableShape and cannot be null"); - this.AdditionalProperties = new Dictionary(); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of Quadrilateral. + public NullableShape(Quadrilateral actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of Triangle. + public NullableShape(Triangle actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } + + + private Object _actualInstance; /// - /// Gets or Sets additional properties + /// Gets or Sets ActualInstance /// - [JsonExtensionData] - public IDictionary AdditionalProperties { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Quadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Triangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Quadrilateral, Triangle"); + } + } + } + + /// + /// Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of Quadrilateral + public Quadrilateral GetQuadrilateral() + { + return (Quadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of Triangle + public Triangle GetTriangle() + { + return (Triangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -82,9 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class NullableShape {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); - sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -93,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Quadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Triangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -127,12 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); - if (this.AdditionalProperties != null) - hashCode = hashCode * 59 + this.AdditionalProperties.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -143,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Pig.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Pig.cs index 2df11b0ead0..5ee747ed943 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Pig.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Pig.cs @@ -30,39 +30,89 @@ namespace Org.OpenAPITools.Model /// Pig /// [DataContract(Name = "Pig")] - [JsonConverter(typeof(JsonSubtypes), "ClassName")] - public partial class Pig : IEquatable, IValidatableObject + public partial class Pig : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Pig() + public Pig() { - this.AdditionalProperties = new Dictionary(); - } - /// - /// Initializes a new instance of the class. - /// - /// className (required). - public Pig(string className = default(string)) - { - // to ensure "className" is required (not null) - this.ClassName = className ?? throw new ArgumentNullException("className is a required property for Pig and cannot be null"); - this.AdditionalProperties = new Dictionary(); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ClassName + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "className", IsRequired = true, EmitDefaultValue = false)] - public string ClassName { get; set; } + /// An instance of BasquePig. + public Pig(BasquePig actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets additional properties + /// Initializes a new instance of the class + /// with the class /// - [JsonExtensionData] - public IDictionary AdditionalProperties { get; set; } + /// An instance of DanishPig. + public Pig(DanishPig actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(BasquePig)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(DanishPig)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: BasquePig, DanishPig"); + } + } + } + + /// + /// Get the actual instance of `BasquePig`. If the actual instanct is not `BasquePig`, + /// the InvalidClassException will be thrown + /// + /// An instance of BasquePig + public BasquePig GetBasquePig() + { + return (BasquePig)this.ActualInstance; + } + + /// + /// Get the actual instance of `DanishPig`. If the actual instanct is not `DanishPig`, + /// the InvalidClassException will be thrown + /// + /// An instance of DanishPig + public DanishPig GetDanishPig() + { + return (DanishPig)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -72,8 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Pig {\n"); - sb.Append(" ClassName: ").Append(ClassName).Append("\n"); - sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -82,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("BasquePig"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("DanishPig"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -116,10 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ClassName != null) - hashCode = hashCode * 59 + this.ClassName.GetHashCode(); - if (this.AdditionalProperties != null) - hashCode = hashCode * 59 + this.AdditionalProperties.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -130,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Quadrilateral.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Quadrilateral.cs index fb70cc4bc5b..64218530654 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Quadrilateral.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Quadrilateral.cs @@ -30,48 +30,89 @@ namespace Org.OpenAPITools.Model /// Quadrilateral /// [DataContract(Name = "Quadrilateral")] - [JsonConverter(typeof(JsonSubtypes), "QuadrilateralType")] - public partial class Quadrilateral : IEquatable, IValidatableObject + public partial class Quadrilateral : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Quadrilateral() + public Quadrilateral() { - this.AdditionalProperties = new Dictionary(); - } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - public Quadrilateral(string shapeType = default(string), string quadrilateralType = default(string)) - { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for Quadrilateral and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for Quadrilateral and cannot be null"); - this.AdditionalProperties = new Dictionary(); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of ComplexQuadrilateral. + public Quadrilateral(ComplexQuadrilateral actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of SimpleQuadrilateral. + public Quadrilateral(SimpleQuadrilateral actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets additional properties + /// Gets or Sets ActualInstance /// - [JsonExtensionData] - public IDictionary AdditionalProperties { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(ComplexQuadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(SimpleQuadrilateral)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: ComplexQuadrilateral, SimpleQuadrilateral"); + } + } + } + + /// + /// Get the actual instance of `ComplexQuadrilateral`. If the actual instanct is not `ComplexQuadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of ComplexQuadrilateral + public ComplexQuadrilateral GetComplexQuadrilateral() + { + return (ComplexQuadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `SimpleQuadrilateral`. If the actual instanct is not `SimpleQuadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of SimpleQuadrilateral + public SimpleQuadrilateral GetSimpleQuadrilateral() + { + return (SimpleQuadrilateral)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -81,9 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Quadrilateral {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); - sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -92,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("ComplexQuadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("SimpleQuadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -126,12 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); - if (this.AdditionalProperties != null) - hashCode = hashCode * 59 + this.AdditionalProperties.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -142,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Shape.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Shape.cs index 106e40be974..cb05de849eb 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Shape.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Shape.cs @@ -30,49 +30,89 @@ namespace Org.OpenAPITools.Model /// Shape /// [DataContract(Name = "Shape")] - [JsonConverter(typeof(JsonSubtypes), "ShapeType")] - public partial class Shape : IEquatable, IValidatableObject + public partial class Shape : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Shape() + public Shape() { - this.AdditionalProperties = new Dictionary(); - } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - /// triangleType (required). - public Shape(string shapeType = default(string), string quadrilateralType = default(string), string triangleType = default(string)) - { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for Shape and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for Shape and cannot be null"); - this.AdditionalProperties = new Dictionary(); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of Quadrilateral. + public Shape(Quadrilateral actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of Triangle. + public Shape(Triangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets additional properties + /// Gets or Sets ActualInstance /// - [JsonExtensionData] - public IDictionary AdditionalProperties { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Quadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Triangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Quadrilateral, Triangle"); + } + } + } + + /// + /// Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of Quadrilateral + public Quadrilateral GetQuadrilateral() + { + return (Quadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of Triangle + public Triangle GetTriangle() + { + return (Triangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -82,9 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Shape {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); - sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -93,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Quadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Triangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -127,12 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); - if (this.AdditionalProperties != null) - hashCode = hashCode * 59 + this.AdditionalProperties.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -143,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/ShapeOrNull.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/ShapeOrNull.cs index 1cc0d862334..f3684a119c7 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/ShapeOrNull.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/ShapeOrNull.cs @@ -30,49 +30,89 @@ namespace Org.OpenAPITools.Model /// The value may be a shape or the 'null' value. This is introduced in OAS schema >= 3.1. /// [DataContract(Name = "ShapeOrNull")] - [JsonConverter(typeof(JsonSubtypes), "ShapeType")] - public partial class ShapeOrNull : IEquatable, IValidatableObject + public partial class ShapeOrNull : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected ShapeOrNull() + public ShapeOrNull() { - this.AdditionalProperties = new Dictionary(); - } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - /// triangleType (required). - public ShapeOrNull(string shapeType = default(string), string quadrilateralType = default(string), string triangleType = default(string)) - { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for ShapeOrNull and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for ShapeOrNull and cannot be null"); - this.AdditionalProperties = new Dictionary(); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of Quadrilateral. + public ShapeOrNull(Quadrilateral actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of Triangle. + public ShapeOrNull(Triangle actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } + + + private Object _actualInstance; /// - /// Gets or Sets additional properties + /// Gets or Sets ActualInstance /// - [JsonExtensionData] - public IDictionary AdditionalProperties { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Quadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Triangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Quadrilateral, Triangle"); + } + } + } + + /// + /// Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of Quadrilateral + public Quadrilateral GetQuadrilateral() + { + return (Quadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of Triangle + public Triangle GetTriangle() + { + return (Triangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -82,9 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class ShapeOrNull {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); - sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -93,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Quadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Triangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -127,12 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); - if (this.AdditionalProperties != null) - hashCode = hashCode * 59 + this.AdditionalProperties.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -143,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Triangle.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Triangle.cs index a3ef3ac5555..e57e11f1868 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Triangle.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/Triangle.cs @@ -30,48 +30,115 @@ namespace Org.OpenAPITools.Model /// Triangle /// [DataContract(Name = "Triangle")] - [JsonConverter(typeof(JsonSubtypes), "TriangleType")] - public partial class Triangle : IEquatable, IValidatableObject + public partial class Triangle : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Triangle() + public Triangle() { - this.AdditionalProperties = new Dictionary(); - } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// triangleType (required). - public Triangle(string shapeType = default(string), string triangleType = default(string)) - { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for Triangle and cannot be null"); - // to ensure "triangleType" is required (not null) - this.TriangleType = triangleType ?? throw new ArgumentNullException("triangleType is a required property for Triangle and cannot be null"); - this.AdditionalProperties = new Dictionary(); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of EquilateralTriangle. + public Triangle(EquilateralTriangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets TriangleType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "triangleType", IsRequired = true, EmitDefaultValue = false)] - public string TriangleType { get; set; } + /// An instance of IsoscelesTriangle. + public Triangle(IsoscelesTriangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets additional properties + /// Initializes a new instance of the class + /// with the class /// - [JsonExtensionData] - public IDictionary AdditionalProperties { get; set; } + /// An instance of ScaleneTriangle. + public Triangle(ScaleneTriangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(EquilateralTriangle)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(IsoscelesTriangle)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(ScaleneTriangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle"); + } + } + } + + /// + /// Get the actual instance of `EquilateralTriangle`. If the actual instanct is not `EquilateralTriangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of EquilateralTriangle + public EquilateralTriangle GetEquilateralTriangle() + { + return (EquilateralTriangle)this.ActualInstance; + } + + /// + /// Get the actual instance of `IsoscelesTriangle`. If the actual instanct is not `IsoscelesTriangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of IsoscelesTriangle + public IsoscelesTriangle GetIsoscelesTriangle() + { + return (IsoscelesTriangle)this.ActualInstance; + } + + /// + /// Get the actual instance of `ScaleneTriangle`. If the actual instanct is not `ScaleneTriangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of ScaleneTriangle + public ScaleneTriangle GetScaleneTriangle() + { + return (ScaleneTriangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -81,9 +148,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Triangle {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" TriangleType: ").Append(TriangleType).Append("\n"); - sb.Append(" AdditionalProperties: ").Append(AdditionalProperties).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -92,9 +157,69 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("EquilateralTriangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("IsoscelesTriangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("ScaleneTriangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -126,12 +251,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.TriangleType != null) - hashCode = hashCode * 59 + this.TriangleType.GetHashCode(); - if (this.AdditionalProperties != null) - hashCode = hashCode * 59 + this.AdditionalProperties.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -142,16 +263,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/.openapi-generator/FILES b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/.openapi-generator/FILES index 28a46aca90c..4894085ce8f 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/.openapi-generator/FILES +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/.openapi-generator/FILES @@ -114,6 +114,7 @@ src/Org.OpenAPITools/Client/Multimap.cs src/Org.OpenAPITools/Client/OpenAPIDateConverter.cs src/Org.OpenAPITools/Client/RequestOptions.cs src/Org.OpenAPITools/Client/RetryConfiguration.cs +src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs src/Org.OpenAPITools/Model/AdditionalPropertiesClass.cs src/Org.OpenAPITools/Model/Animal.cs src/Org.OpenAPITools/Model/ApiResponse.cs diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs index df3f97f8b81..639b97f9e82 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -13,15 +13,16 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Web; using System.Linq; using System.Net; +using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Text; using System.Threading; using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Web; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using RestSharp; @@ -65,8 +66,15 @@ namespace Org.OpenAPITools.Client public string Serialize(object obj) { - var result = JsonConvert.SerializeObject(obj, _serializerSettings); - return result; + if (obj != null && obj.GetType().IsInstanceOfType(typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema))) + { + // the object to be serialized is an oneOf/anyOf schema + return ((Org.OpenAPITools.Model.AbstractOpenAPISchema)obj).ToJson(); + } + else + { + return JsonConvert.SerializeObject(obj, _serializerSettings); + } } public T Deserialize(IRestResponse response) @@ -126,7 +134,17 @@ namespace Org.OpenAPITools.Client // at this point, it must be a model (json) try { - return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings); + if (type.IsInstanceOfType(typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema))) + { + // the response is an oneOf/anyOf schema + Org.OpenAPITools.Model.AbstractOpenAPISchema instance = (Org.OpenAPITools.Model.AbstractOpenAPISchema)Activator.CreateInstance(type); + instance.FromJson(response.Content); + return instance; + } + else + { + return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings); + } } catch (Exception e) { @@ -442,6 +460,15 @@ namespace Org.OpenAPITools.Client response = client.Execute(req); } + // if the response type is oneOf/anyOf, call FromJSON to deserialize the data + if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) + { + T instance = (T)Activator.CreateInstance(typeof(T)); + MethodInfo method = typeof(T).GetMethod("FromJson"); + method.Invoke(instance, new object[] { response.Content }); + response.Data = instance; + } + InterceptResponse(req, response); var result = ToApiResponse(response); @@ -539,6 +566,15 @@ namespace Org.OpenAPITools.Client response = await client.ExecuteAsync(req, cancellationToken).ConfigureAwait(false); } + // if the response type is oneOf/anyOf, call FromJSON to deserialize the data + if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) + { + T instance = (T)Activator.CreateInstance(typeof(T)); + MethodInfo method = typeof(T).GetMethod("FromJson"); + method.Invoke(instance, new object[] { response.Content }); + response.Data = instance; + } + InterceptResponse(req, response); var result = ToApiResponse(response); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs new file mode 100644 index 00000000000..718110f9ac7 --- /dev/null +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/AbstractOpenAPISchema.cs @@ -0,0 +1,61 @@ +/* + * OpenAPI Petstore + * + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * The version of the OpenAPI document: 1.0.0 + * Generated by: https://github.com/openapitools/openapi-generator.git + */ + + +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace Org.OpenAPITools.Model +{ + /// + /// Abstract base class for oneOf, anyOf schemas in the OpenAPI specification + /// + public abstract partial class AbstractOpenAPISchema + { + protected readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings + { + // OpenAPI generated types generally hide default constructors. + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, + MissingMemberHandling = MissingMemberHandling.Error, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy + { + OverrideSpecifiedNames = false + } + } + }; + + /// + /// Gets or Sets the actual instance + /// + public abstract Object ActualInstance { get; set; } + + /// + /// Gets or Sets IsNullable to indicate whether the instance is nullable + /// + public bool IsNullable { get; protected set; } + + /// + /// Gets or Sets the schema type, which can be either `oneOf` or `anyOf` + /// + public string SchemaType { get; protected set; } + + /// + /// Converts the instance into JSON string. + /// + public abstract string ToJson(); + + /// + /// Converts the JSON string into the instance + /// + public abstract void FromJson(string jsonString); + } +} diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Fruit.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Fruit.cs index 7dcf395dbad..e8cf40b9d52 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Fruit.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Fruit.cs @@ -29,46 +29,89 @@ namespace Org.OpenAPITools.Model /// Fruit /// [DataContract(Name = "fruit")] - public partial class Fruit : IEquatable, IValidatableObject + public partial class Fruit : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - /// color. - /// cultivar. - /// origin. - /// lengthCm. - public Fruit(string color = default(string), string cultivar = default(string), string origin = default(string), decimal lengthCm = default(decimal)) + public Fruit() { - this.Color = color; - this.Cultivar = cultivar; - this.Origin = origin; - this.LengthCm = lengthCm; + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets Color + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "color", EmitDefaultValue = false)] - public string Color { get; set; } + /// An instance of Apple. + public Fruit(Apple actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets Cultivar + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "cultivar", EmitDefaultValue = false)] - public string Cultivar { get; set; } + /// An instance of Banana. + public Fruit(Banana actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets Origin + /// Gets or Sets ActualInstance /// - [DataMember(Name = "origin", EmitDefaultValue = false)] - public string Origin { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Apple)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Banana)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Apple, Banana"); + } + } + } /// - /// Gets or Sets LengthCm + /// Get the actual instance of `Apple`. If the actual instanct is not `Apple`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "lengthCm", EmitDefaultValue = false)] - public decimal LengthCm { get; set; } + /// An instance of Apple + public Apple GetApple() + { + return (Apple)this.ActualInstance; + } + + /// + /// Get the actual instance of `Banana`. If the actual instanct is not `Banana`, + /// the InvalidClassException will be thrown + /// + /// An instance of Banana + public Banana GetBanana() + { + return (Banana)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -78,10 +121,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Fruit {\n"); - sb.Append(" Color: ").Append(Color).Append("\n"); - sb.Append(" Cultivar: ").Append(Cultivar).Append("\n"); - sb.Append(" Origin: ").Append(Origin).Append("\n"); - sb.Append(" LengthCm: ").Append(LengthCm).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -90,9 +130,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Apple"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Banana"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -124,13 +211,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.Color != null) - hashCode = hashCode * 59 + this.Color.GetHashCode(); - if (this.Cultivar != null) - hashCode = hashCode * 59 + this.Cultivar.GetHashCode(); - if (this.Origin != null) - hashCode = hashCode * 59 + this.Origin.GetHashCode(); - hashCode = hashCode * 59 + this.LengthCm.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -142,20 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) { - // Cultivar (string) pattern - Regex regexCultivar = new Regex(@"^[a-zA-Z\\s]*$", RegexOptions.CultureInvariant); - if (false == regexCultivar.Match(this.Cultivar).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Cultivar, must match a pattern of " + regexCultivar, new [] { "Cultivar" }); - } - - // Origin (string) pattern - Regex regexOrigin = new Regex(@"^[A-Z\\s]*$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); - if (false == regexOrigin.Match(this.Origin).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Origin, must match a pattern of " + regexOrigin, new [] { "Origin" }); - } - yield break; } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/FruitReq.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/FruitReq.cs index 9d0eafa09b7..c1604f4ae86 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/FruitReq.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/FruitReq.cs @@ -29,52 +29,89 @@ namespace Org.OpenAPITools.Model /// FruitReq /// [DataContract(Name = "fruitReq")] - public partial class FruitReq : IEquatable, IValidatableObject + public partial class FruitReq : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected FruitReq() { } - /// - /// Initializes a new instance of the class. - /// - /// cultivar (required). - /// mealy. - /// lengthCm (required). - /// sweet. - public FruitReq(string cultivar = default(string), bool mealy = default(bool), decimal lengthCm = default(decimal), bool sweet = default(bool)) + public FruitReq() { - // to ensure "cultivar" is required (not null) - this.Cultivar = cultivar ?? throw new ArgumentNullException("cultivar is a required property for FruitReq and cannot be null"); - this.LengthCm = lengthCm; - this.Mealy = mealy; - this.Sweet = sweet; + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets Cultivar + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "cultivar", IsRequired = true, EmitDefaultValue = false)] - public string Cultivar { get; set; } + /// An instance of AppleReq. + public FruitReq(AppleReq actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } /// - /// Gets or Sets Mealy + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "mealy", EmitDefaultValue = false)] - public bool Mealy { get; set; } + /// An instance of BananaReq. + public FruitReq(BananaReq actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } + + + private Object _actualInstance; /// - /// Gets or Sets LengthCm + /// Gets or Sets ActualInstance /// - [DataMember(Name = "lengthCm", IsRequired = true, EmitDefaultValue = false)] - public decimal LengthCm { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(AppleReq)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(BananaReq)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: AppleReq, BananaReq"); + } + } + } /// - /// Gets or Sets Sweet + /// Get the actual instance of `AppleReq`. If the actual instanct is not `AppleReq`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "sweet", EmitDefaultValue = false)] - public bool Sweet { get; set; } + /// An instance of AppleReq + public AppleReq GetAppleReq() + { + return (AppleReq)this.ActualInstance; + } + + /// + /// Get the actual instance of `BananaReq`. If the actual instanct is not `BananaReq`, + /// the InvalidClassException will be thrown + /// + /// An instance of BananaReq + public BananaReq GetBananaReq() + { + return (BananaReq)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -84,10 +121,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class FruitReq {\n"); - sb.Append(" Cultivar: ").Append(Cultivar).Append("\n"); - sb.Append(" Mealy: ").Append(Mealy).Append("\n"); - sb.Append(" LengthCm: ").Append(LengthCm).Append("\n"); - sb.Append(" Sweet: ").Append(Sweet).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -96,9 +130,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("AppleReq"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("BananaReq"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -130,11 +211,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.Cultivar != null) - hashCode = hashCode * 59 + this.Cultivar.GetHashCode(); - hashCode = hashCode * 59 + this.Mealy.GetHashCode(); - hashCode = hashCode * 59 + this.LengthCm.GetHashCode(); - hashCode = hashCode * 59 + this.Sweet.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Mammal.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Mammal.cs index 5df726a223e..15ac75fcbfb 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Mammal.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Mammal.cs @@ -30,78 +30,115 @@ namespace Org.OpenAPITools.Model /// Mammal /// [DataContract(Name = "mammal")] - [JsonConverter(typeof(JsonSubtypes), "ClassName")] - public partial class Mammal : IEquatable, IValidatableObject + public partial class Mammal : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// - /// Defines Type + /// Initializes a new instance of the class. /// - [JsonConverter(typeof(StringEnumConverter))] - public enum TypeEnum + public Mammal() { - /// - /// Enum Plains for value: plains - /// - [EnumMember(Value = "plains")] - Plains = 1, - - /// - /// Enum Mountain for value: mountain - /// - [EnumMember(Value = "mountain")] - Mountain = 2, - - /// - /// Enum Grevys for value: grevys - /// - [EnumMember(Value = "grevys")] - Grevys = 3 - + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets Type + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "type", EmitDefaultValue = false)] - public TypeEnum? Type { get; set; } - /// - /// Initializes a new instance of the class. - /// - [JsonConstructorAttribute] - protected Mammal() { } - /// - /// Initializes a new instance of the class. - /// - /// hasBaleen. - /// hasTeeth. - /// className (required). - /// type. - public Mammal(bool hasBaleen = default(bool), bool hasTeeth = default(bool), string className = default(string), TypeEnum? type = default(TypeEnum?)) + /// An instance of Pig. + public Mammal(Pig actualInstance) { - // to ensure "className" is required (not null) - this.ClassName = className ?? throw new ArgumentNullException("className is a required property for Mammal and cannot be null"); - this.HasBaleen = hasBaleen; - this.HasTeeth = hasTeeth; - this.Type = type; + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); } /// - /// Gets or Sets HasBaleen + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "hasBaleen", EmitDefaultValue = false)] - public bool HasBaleen { get; set; } + /// An instance of Whale. + public Mammal(Whale actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets HasTeeth + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "hasTeeth", EmitDefaultValue = false)] - public bool HasTeeth { get; set; } + /// An instance of Zebra. + public Mammal(Zebra actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets ClassName + /// Gets or Sets ActualInstance /// - [DataMember(Name = "className", IsRequired = true, EmitDefaultValue = false)] - public string ClassName { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Pig)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Whale)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Zebra)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Pig, Whale, Zebra"); + } + } + } + + /// + /// Get the actual instance of `Pig`. If the actual instanct is not `Pig`, + /// the InvalidClassException will be thrown + /// + /// An instance of Pig + public Pig GetPig() + { + return (Pig)this.ActualInstance; + } + + /// + /// Get the actual instance of `Whale`. If the actual instanct is not `Whale`, + /// the InvalidClassException will be thrown + /// + /// An instance of Whale + public Whale GetWhale() + { + return (Whale)this.ActualInstance; + } + + /// + /// Get the actual instance of `Zebra`. If the actual instanct is not `Zebra`, + /// the InvalidClassException will be thrown + /// + /// An instance of Zebra + public Zebra GetZebra() + { + return (Zebra)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -111,10 +148,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Mammal {\n"); - sb.Append(" HasBaleen: ").Append(HasBaleen).Append("\n"); - sb.Append(" HasTeeth: ").Append(HasTeeth).Append("\n"); - sb.Append(" ClassName: ").Append(ClassName).Append("\n"); - sb.Append(" Type: ").Append(Type).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -123,9 +157,69 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Pig"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Whale"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Zebra"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -157,11 +251,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - hashCode = hashCode * 59 + this.HasBaleen.GetHashCode(); - hashCode = hashCode * 59 + this.HasTeeth.GetHashCode(); - if (this.ClassName != null) - hashCode = hashCode * 59 + this.ClassName.GetHashCode(); - hashCode = hashCode * 59 + this.Type.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -172,16 +263,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/NullableShape.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/NullableShape.cs index 437788c23e4..03e206f92ad 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/NullableShape.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/NullableShape.cs @@ -30,39 +30,89 @@ namespace Org.OpenAPITools.Model /// The value may be a shape or the 'null' value. The 'nullable' attribute was introduced in OAS schema >= 3.0 and has been deprecated in OAS schema >= 3.1. /// [DataContract(Name = "NullableShape")] - [JsonConverter(typeof(JsonSubtypes), "ShapeType")] - public partial class NullableShape : IEquatable, IValidatableObject + public partial class NullableShape : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected NullableShape() { } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - /// triangleType (required). - public NullableShape(string shapeType = default(string), string quadrilateralType = default(string), string triangleType = default(string)) + public NullableShape() { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for NullableShape and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for NullableShape and cannot be null"); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of Quadrilateral. + public NullableShape(Quadrilateral actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of Triangle. + public NullableShape(Triangle actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Quadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Triangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Quadrilateral, Triangle"); + } + } + } + + /// + /// Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of Quadrilateral + public Quadrilateral GetQuadrilateral() + { + return (Quadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of Triangle + public Triangle GetTriangle() + { + return (Triangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -72,8 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class NullableShape {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -82,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Quadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Triangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -116,10 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -130,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Pig.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Pig.cs index c1305d123c9..5ee747ed943 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Pig.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Pig.cs @@ -30,29 +30,89 @@ namespace Org.OpenAPITools.Model /// Pig /// [DataContract(Name = "Pig")] - [JsonConverter(typeof(JsonSubtypes), "ClassName")] - public partial class Pig : IEquatable, IValidatableObject + public partial class Pig : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Pig() { } - /// - /// Initializes a new instance of the class. - /// - /// className (required). - public Pig(string className = default(string)) + public Pig() { - // to ensure "className" is required (not null) - this.ClassName = className ?? throw new ArgumentNullException("className is a required property for Pig and cannot be null"); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ClassName + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "className", IsRequired = true, EmitDefaultValue = false)] - public string ClassName { get; set; } + /// An instance of BasquePig. + public Pig(BasquePig actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + /// + /// Initializes a new instance of the class + /// with the class + /// + /// An instance of DanishPig. + public Pig(DanishPig actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(BasquePig)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(DanishPig)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: BasquePig, DanishPig"); + } + } + } + + /// + /// Get the actual instance of `BasquePig`. If the actual instanct is not `BasquePig`, + /// the InvalidClassException will be thrown + /// + /// An instance of BasquePig + public BasquePig GetBasquePig() + { + return (BasquePig)this.ActualInstance; + } + + /// + /// Get the actual instance of `DanishPig`. If the actual instanct is not `DanishPig`, + /// the InvalidClassException will be thrown + /// + /// An instance of DanishPig + public DanishPig GetDanishPig() + { + return (DanishPig)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -62,7 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Pig {\n"); - sb.Append(" ClassName: ").Append(ClassName).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -71,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("BasquePig"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("DanishPig"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -105,8 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ClassName != null) - hashCode = hashCode * 59 + this.ClassName.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -117,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Quadrilateral.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Quadrilateral.cs index 43cd1e7116c..64218530654 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Quadrilateral.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Quadrilateral.cs @@ -30,38 +30,89 @@ namespace Org.OpenAPITools.Model /// Quadrilateral /// [DataContract(Name = "Quadrilateral")] - [JsonConverter(typeof(JsonSubtypes), "QuadrilateralType")] - public partial class Quadrilateral : IEquatable, IValidatableObject + public partial class Quadrilateral : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Quadrilateral() { } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - public Quadrilateral(string shapeType = default(string), string quadrilateralType = default(string)) + public Quadrilateral() { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for Quadrilateral and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for Quadrilateral and cannot be null"); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of ComplexQuadrilateral. + public Quadrilateral(ComplexQuadrilateral actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of SimpleQuadrilateral. + public Quadrilateral(SimpleQuadrilateral actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(ComplexQuadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(SimpleQuadrilateral)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: ComplexQuadrilateral, SimpleQuadrilateral"); + } + } + } + + /// + /// Get the actual instance of `ComplexQuadrilateral`. If the actual instanct is not `ComplexQuadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of ComplexQuadrilateral + public ComplexQuadrilateral GetComplexQuadrilateral() + { + return (ComplexQuadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `SimpleQuadrilateral`. If the actual instanct is not `SimpleQuadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of SimpleQuadrilateral + public SimpleQuadrilateral GetSimpleQuadrilateral() + { + return (SimpleQuadrilateral)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -71,8 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Quadrilateral {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -81,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("ComplexQuadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("SimpleQuadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -115,10 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -129,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Shape.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Shape.cs index 3a1bb41840c..cb05de849eb 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Shape.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Shape.cs @@ -30,39 +30,89 @@ namespace Org.OpenAPITools.Model /// Shape /// [DataContract(Name = "Shape")] - [JsonConverter(typeof(JsonSubtypes), "ShapeType")] - public partial class Shape : IEquatable, IValidatableObject + public partial class Shape : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Shape() { } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - /// triangleType (required). - public Shape(string shapeType = default(string), string quadrilateralType = default(string), string triangleType = default(string)) + public Shape() { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for Shape and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for Shape and cannot be null"); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of Quadrilateral. + public Shape(Quadrilateral actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of Triangle. + public Shape(Triangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Quadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Triangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Quadrilateral, Triangle"); + } + } + } + + /// + /// Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of Quadrilateral + public Quadrilateral GetQuadrilateral() + { + return (Quadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of Triangle + public Triangle GetTriangle() + { + return (Triangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -72,8 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Shape {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -82,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Quadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Triangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -116,10 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -130,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/ShapeOrNull.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/ShapeOrNull.cs index be6e6427390..f3684a119c7 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/ShapeOrNull.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/ShapeOrNull.cs @@ -30,39 +30,89 @@ namespace Org.OpenAPITools.Model /// The value may be a shape or the 'null' value. This is introduced in OAS schema >= 3.1. /// [DataContract(Name = "ShapeOrNull")] - [JsonConverter(typeof(JsonSubtypes), "ShapeType")] - public partial class ShapeOrNull : IEquatable, IValidatableObject + public partial class ShapeOrNull : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected ShapeOrNull() { } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// quadrilateralType (required). - /// triangleType (required). - public ShapeOrNull(string shapeType = default(string), string quadrilateralType = default(string), string triangleType = default(string)) + public ShapeOrNull() { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for ShapeOrNull and cannot be null"); - // to ensure "quadrilateralType" is required (not null) - this.QuadrilateralType = quadrilateralType ?? throw new ArgumentNullException("quadrilateralType is a required property for ShapeOrNull and cannot be null"); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of Quadrilateral. + public ShapeOrNull(Quadrilateral actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } /// - /// Gets or Sets QuadrilateralType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "quadrilateralType", IsRequired = true, EmitDefaultValue = false)] - public string QuadrilateralType { get; set; } + /// An instance of Triangle. + public ShapeOrNull(Triangle actualInstance) + { + this.IsNullable = true; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance; + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Quadrilateral)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Triangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Quadrilateral, Triangle"); + } + } + } + + /// + /// Get the actual instance of `Quadrilateral`. If the actual instanct is not `Quadrilateral`, + /// the InvalidClassException will be thrown + /// + /// An instance of Quadrilateral + public Quadrilateral GetQuadrilateral() + { + return (Quadrilateral)this.ActualInstance; + } + + /// + /// Get the actual instance of `Triangle`. If the actual instanct is not `Triangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of Triangle + public Triangle GetTriangle() + { + return (Triangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -72,8 +122,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class ShapeOrNull {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" QuadrilateralType: ").Append(QuadrilateralType).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -82,9 +131,56 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Quadrilateral"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("Triangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -116,10 +212,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.QuadrilateralType != null) - hashCode = hashCode * 59 + this.QuadrilateralType.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -130,16 +224,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Triangle.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Triangle.cs index bc2cbe390bb..e57e11f1868 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Triangle.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/Triangle.cs @@ -30,38 +30,115 @@ namespace Org.OpenAPITools.Model /// Triangle /// [DataContract(Name = "Triangle")] - [JsonConverter(typeof(JsonSubtypes), "TriangleType")] - public partial class Triangle : IEquatable, IValidatableObject + public partial class Triangle : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// /// Initializes a new instance of the class. /// - [JsonConstructorAttribute] - protected Triangle() { } - /// - /// Initializes a new instance of the class. - /// - /// shapeType (required). - /// triangleType (required). - public Triangle(string shapeType = default(string), string triangleType = default(string)) + public Triangle() { - // to ensure "shapeType" is required (not null) - this.ShapeType = shapeType ?? throw new ArgumentNullException("shapeType is a required property for Triangle and cannot be null"); - // to ensure "triangleType" is required (not null) - this.TriangleType = triangleType ?? throw new ArgumentNullException("triangleType is a required property for Triangle and cannot be null"); + this.IsNullable = true; + this.SchemaType= "oneOf"; } /// - /// Gets or Sets ShapeType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "shapeType", IsRequired = true, EmitDefaultValue = false)] - public string ShapeType { get; set; } + /// An instance of EquilateralTriangle. + public Triangle(EquilateralTriangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } /// - /// Gets or Sets TriangleType + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "triangleType", IsRequired = true, EmitDefaultValue = false)] - public string TriangleType { get; set; } + /// An instance of IsoscelesTriangle. + public Triangle(IsoscelesTriangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + /// + /// Initializes a new instance of the class + /// with the class + /// + /// An instance of ScaleneTriangle. + public Triangle(ScaleneTriangle actualInstance) + { + this.IsNullable = false; + this.SchemaType= "oneOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(EquilateralTriangle)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(IsoscelesTriangle)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(ScaleneTriangle)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle"); + } + } + } + + /// + /// Get the actual instance of `EquilateralTriangle`. If the actual instanct is not `EquilateralTriangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of EquilateralTriangle + public EquilateralTriangle GetEquilateralTriangle() + { + return (EquilateralTriangle)this.ActualInstance; + } + + /// + /// Get the actual instance of `IsoscelesTriangle`. If the actual instanct is not `IsoscelesTriangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of IsoscelesTriangle + public IsoscelesTriangle GetIsoscelesTriangle() + { + return (IsoscelesTriangle)this.ActualInstance; + } + + /// + /// Get the actual instance of `ScaleneTriangle`. If the actual instanct is not `ScaleneTriangle`, + /// the InvalidClassException will be thrown + /// + /// An instance of ScaleneTriangle + public ScaleneTriangle GetScaleneTriangle() + { + return (ScaleneTriangle)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -71,8 +148,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class Triangle {\n"); - sb.Append(" ShapeType: ").Append(ShapeType).Append("\n"); - sb.Append(" TriangleType: ").Append(TriangleType).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -81,9 +157,69 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, _serializerSettings); + } + + /// + /// Converts the JSON string into the object + /// + /// JSON string + public override void FromJson(string jsonString) + { + int match = 0; + List matchedTypes = new List(); + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("EquilateralTriangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("IsoscelesTriangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + try + { + this.ActualInstance = JsonConvert.DeserializeObject(jsonString, _serializerSettings); + matchedTypes.Add("ScaleneTriangle"); + match++; + } + catch (Exception exception) + { + // deserialization failed, try the next one + // uncomment the line below for troubleshooting + //Console.WriteLine(exception.ToString()); + } + + if (match == 0) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + else if (match > 1) + { + throw new InvalidDataException("The JSON string `" + jsonString + "` incorrectly matches more than one schema (should be exactly one match): " + matchedTypes); + } + + // deserialization is considered successful at this point if no exception has been thrown. } /// @@ -115,10 +251,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.ShapeType != null) - hashCode = hashCode * 59 + this.ShapeType.GetHashCode(); - if (this.TriangleType != null) - hashCode = hashCode * 59 + this.TriangleType.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -129,16 +263,6 @@ namespace Org.OpenAPITools.Model /// Validation context /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) - { - return this.BaseValidate(validationContext); - } - - /// - /// To validate all properties of the instance - /// - /// Validation context - /// Validation Result - protected IEnumerable BaseValidate(ValidationContext validationContext) { yield break; } diff --git a/samples/client/petstore/ruby/Gemfile.lock b/samples/client/petstore/ruby/Gemfile.lock new file mode 100644 index 00000000000..f6aa4f5abfe --- /dev/null +++ b/samples/client/petstore/ruby/Gemfile.lock @@ -0,0 +1,68 @@ +PATH + remote: . + specs: + petstore (1.0.0) + typhoeus (~> 1.0, >= 1.0.1) + +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.1) + byebug (11.1.3) + coderay (1.1.3) + diff-lcs (1.4.4) + ethon (0.12.0) + ffi (>= 1.3.0) + ffi (1.13.1) + jaro_winkler (1.5.4) + method_source (1.0.0) + parallel (1.19.2) + parser (2.7.1.5) + ast (~> 2.4.1) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.9.0) + byebug (~> 11.0) + pry (~> 0.13.0) + psych (3.2.0) + rainbow (3.0.0) + rake (13.0.1) + rspec (3.9.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-core (3.9.2) + rspec-support (~> 3.9.3) + rspec-expectations (3.9.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.3) + rubocop (0.66.0) + jaro_winkler (~> 1.5.1) + parallel (~> 1.10) + parser (>= 2.5, != 2.5.1.1) + psych (>= 3.1.0) + rainbow (>= 2.2.2, < 4.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 1.6) + ruby-progressbar (1.10.1) + typhoeus (1.4.0) + ethon (>= 0.9.0) + unicode-display_width (1.5.0) + +PLATFORMS + ruby + +DEPENDENCIES + petstore! + pry-byebug + rake (~> 13.0.1) + rspec (~> 3.6, >= 3.6.0) + rubocop (~> 0.66.0) + +BUNDLED WITH + 1.17.2