private static CodegenType MakeDictionaryType(string name, Schema schema) { var returnType = new CodegenType(); if (schema.AdditionalProperties.Type.Count > 1) { returnType.CodeType = new CodeTypeReference(typeof(Dictionary <string, object>)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } if (schema.HasDefaultValue()) { throw new NotImplementedException("Defaults for dictionaries are not yet supported"); } if (schema.AdditionalProperties.Type[0].Name == "object") { if (schema.AdditionalProperties.Title != null) { returnType.CodeType = new CodeTypeReference($"System.Collections.Generic.Dictionary<string, {Helpers.ParseTitle(schema.AdditionalProperties.Title)}>"); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } returnType.CodeType = new CodeTypeReference(typeof(Dictionary <string, object>)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } if (schema.AdditionalProperties.Type[0].Name == "string") { returnType.CodeType = new CodeTypeReference(typeof(Dictionary <string, string>)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } if (schema.AdditionalProperties.Type[0].Name == "integer") { returnType.CodeType = new CodeTypeReference(typeof(Dictionary <string, int>)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } throw new NotImplementedException($"Dictionary<string,{schema.AdditionalProperties.Type[0].Name}> not yet implemented."); }
public static CodegenType MakeCodegenType(string name, Schema schema) { if (!(schema.Items?.Type?.Count > 0)) { throw new InvalidOperationException("Array type must contain an item type"); } if (schema.Enum != null) { throw new InvalidOperationException(); } var returnType = new CodegenType(); returnType.Attributes.Add(new CodeAttributeDeclaration("Newtonsoft.Json.JsonConverterAttribute", new[] { new CodeAttributeArgument(new CodeTypeOfExpression(typeof(ArrayConverter))) })); if (schema.Items.Type.Count > 1) { returnType.CodeType = new CodeTypeReference(typeof(object[])); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } EnforceRestrictionsOnSetValues(returnType, name, schema); if (schema.Items.Type[0].Name == "integer") { if (schema.Items.Enum != null) { throw new NotImplementedException(); } if (schema.HasDefaultValue()) { var defaultValueArray = ((JArray)schema.Default).Select( x => (CodeExpression) new CodePrimitiveExpression((int)(long)x)).ToArray(); returnType.DefaultValue = new CodeArrayCreateExpression(typeof(int), defaultValueArray); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheArrayOfValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else { returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); } returnType.CodeType = new CodeTypeReference(typeof(int[])); return(returnType); } if (schema.Items.Enum != null) { throw new NotImplementedException(); } if (schema.Items.Type[0].Name == "number") { if (schema.HasDefaultValue()) { var defaultVauleArray = (JArray)schema.Default; returnType.DefaultValue = new CodeArrayCreateExpression(typeof(float), defaultVauleArray.Select(x => (CodeExpression) new CodePrimitiveExpression((float)x)).ToArray()); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheArrayOfValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else { returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); } returnType.CodeType = new CodeTypeReference(typeof(float[])); return(returnType); } if (schema.Items.Minimum != null || schema.Items.Maximum != null) { throw new NotImplementedException(); } if (schema.Items.Type[0].Name == "boolean") { if (schema.HasDefaultValue()) { var defaultVauleArray = (JArray)schema.Default; returnType.DefaultValue = new CodeArrayCreateExpression(typeof(bool), defaultVauleArray.Select(x => (CodeExpression) new CodePrimitiveExpression((bool)x)).ToArray()); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheArrayOfValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else { returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); } returnType.CodeType = new CodeTypeReference(typeof(bool[])); return(returnType); } if (schema.Items.Type[0].Name == "string") { if (schema.HasDefaultValue()) { var defaultVauleArray = (JArray)schema.Default; returnType.DefaultValue = new CodeArrayCreateExpression(typeof(string), defaultVauleArray.Select(x => (CodeExpression) new CodePrimitiveExpression((string)x)) .ToArray()); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheArrayOfValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else { returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); } returnType.CodeType = new CodeTypeReference(typeof(string[])); return(returnType); } if (schema.Items.Type[0].Name == "object") { if (schema.HasDefaultValue()) { throw new NotImplementedException("Array of Objects has default value"); } if (schema.Items.Title != null) { returnType.CodeType = new CodeTypeReference(Helpers.ParseTitle(schema.Items.Title) + "[]"); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); returnType.Attributes.Clear(); if (schema.Items.MinLength != null || schema.Items.MaxLength != null) { throw new NotImplementedException(); } return(returnType); } if (schema.Items.AdditionalProperties != null) { if (schema.Items.AdditionalProperties.Type[0].Name == "integer") { returnType.CodeType = new CodeTypeReference(typeof(Dictionary <string, int>[])); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); returnType.Attributes.Clear(); return(returnType); } throw new NotImplementedException($"Dictionary<string,{schema.Items.AdditionalProperties.Type[0].Name}> not yet implemented."); } returnType.CodeType = new CodeTypeReference(typeof(object[])); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } throw new NotImplementedException("Array of " + schema.Items.Type[0].Name); }
private static void EnforceRestrictionsOnSetValues(CodegenType returnType, string name, Schema schema) { if (!schema.HasDefaultValue()) { var fieldName = Helpers.GetFieldName(name); returnType.SetStatements.Add(new CodeConditionStatement { Condition = new CodeBinaryOperatorExpression { Left = new CodePropertySetValueReferenceExpression(), Operator = CodeBinaryOperatorType.ValueEquality, Right = new CodePrimitiveExpression(null) }, TrueStatements = { new CodeAssignStatement() { Left = new CodeFieldReferenceExpression { FieldName = fieldName, TargetObject = new CodeThisReferenceExpression() }, Right = new CodePropertySetValueReferenceExpression() }, new CodeMethodReturnStatement() } }); } if (schema.MinItems != null) { returnType.SetStatements.Add(new CodeConditionStatement { Condition = new CodeBinaryOperatorExpression { Left = new CodePropertyReferenceExpression(new CodePropertySetValueReferenceExpression(), "Length"), Operator = CodeBinaryOperatorType.LessThan, Right = new CodePrimitiveExpression(schema.MinItems) }, TrueStatements = { new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof(ArgumentException), new CodeExpression[] { new CodePrimitiveExpression("Array not long enough") })) } }); } if (schema.MaxItems != null) { returnType.SetStatements.Add(new CodeConditionStatement { Condition = new CodeBinaryOperatorExpression { Left = new CodePropertyReferenceExpression(new CodePropertySetValueReferenceExpression(), "Length"), Operator = CodeBinaryOperatorType.GreaterThan, Right = new CodePrimitiveExpression(schema.MaxItems) }, TrueStatements = { new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof(ArgumentException), new CodeExpression[] { new CodePrimitiveExpression("Array too long") })) } }); } if (schema.Items.Minimum != null || schema.Items.Maximum != null || schema.Items.MinLength != null || schema.Items.MaxLength != null) { returnType.SetStatements.Add(new CodeVariableDeclarationStatement(typeof(int), "index", new CodePrimitiveExpression(0))); } if (schema.Items.Minimum != null) { returnType.SetStatements.Add(LoopThroughArray(new CodePropertySetValueReferenceExpression(), new CodeStatementCollection { new CodeConditionStatement { Condition = new CodeBinaryOperatorExpression { Left = new CodeArrayIndexerExpression { TargetObject = new CodePropertySetValueReferenceExpression(), Indices = { new CodeVariableReferenceExpression("index") }, }, Operator = schema.Items.ExclusiveMinimum ? CodeBinaryOperatorType.LessThanOrEqual : CodeBinaryOperatorType.LessThan, Right = new CodePrimitiveExpression(schema.Items.Minimum) }, TrueStatements = { new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof(ArgumentOutOfRangeException))) } } })); } if (schema.Items.Maximum != null) { returnType.SetStatements.Add(LoopThroughArray(new CodePropertySetValueReferenceExpression(), new CodeStatementCollection { new CodeConditionStatement { Condition = new CodeBinaryOperatorExpression { Left = new CodeArrayIndexerExpression { TargetObject = new CodePropertySetValueReferenceExpression(), Indices = { new CodeVariableReferenceExpression("index") }, }, Operator = schema.Items.ExclusiveMaximum ? CodeBinaryOperatorType.GreaterThanOrEqual : CodeBinaryOperatorType.GreaterThan, Right = new CodePrimitiveExpression(schema.Items.Maximum) }, TrueStatements = { new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof(ArgumentOutOfRangeException))) } } })); } if (schema.Items.MinLength != null) { throw new NotImplementedException(); } if (schema.Items.MaxLength != null) { throw new NotImplementedException(); } }
public static CodegenType MakeCodegenType(string name, Schema schema) { CodegenType returnType = new CodegenType(); EnforceRestrictionsOnSetValues(returnType, name, schema); if (schema.Format == "uriref") { switch (schema.UriType) { case UriType.Application: case UriType.Image: case UriType.Text: returnType.CodeType = new CodeTypeReference(typeof(string)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); break; case UriType.None: throw new InvalidDataException("UriType must be specified in the schema"); } return(returnType); } if (schema.Type.Count > 1) { returnType.CodeType = new CodeTypeReference(typeof(object)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } var typeRef = schema.Type[0]; if (typeRef.IsReference) { throw new NotImplementedException(); } if (typeRef.Name == "object") { if (schema.Enum != null || schema.HasDefaultValue()) { throw new NotImplementedException(); } if (schema.Title != null) { returnType.CodeType = new CodeTypeReference(Helpers.ParseTitle(schema.Title)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } throw new NotImplementedException(); } if (typeRef.Name == "number") { if (schema.Enum != null) { throw new NotImplementedException(); } if (schema.HasDefaultValue()) { returnType.DefaultValue = new CodePrimitiveExpression((float)(double)schema.Default); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else if (!schema.IsRequired) { returnType.CodeType = new CodeTypeReference(typeof(float?)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } returnType.CodeType = new CodeTypeReference(typeof(float)); return(returnType); } if (typeRef.Name == "string") { if (schema.Enum != null) { returnType.Attributes.Add( new CodeAttributeDeclaration("Newtonsoft.Json.JsonConverterAttribute", new[] { new CodeAttributeArgument(new CodeTypeOfExpression(typeof(StringEnumConverter))) })); var enumType = GenStringEnumType(name, schema); returnType.AdditionalMembers.Add(enumType); if (schema.HasDefaultValue()) { returnType.CodeType = new CodeTypeReference(enumType.Name); for (var i = 0; i < enumType.Members.Count; i++) { if (enumType.Members[i].Name == schema.Default.ToString()) { returnType.DefaultValue = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(enumType.Name), (string)schema.Default); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); return(returnType); } } throw new InvalidDataException("The default value is not in the enum list"); } else if (!schema.IsRequired) { returnType.CodeType = new CodeTypeReference(typeof(Nullable <>)); returnType.CodeType.TypeArguments.Add(new CodeTypeReference(enumType.Name)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } returnType.CodeType = new CodeTypeReference(enumType.Name); return(returnType); } if (schema.HasDefaultValue()) { returnType.DefaultValue = new CodePrimitiveExpression((string)schema.Default); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else { returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); } returnType.CodeType = new CodeTypeReference(typeof(string)); return(returnType); } if (typeRef.Name == "integer") { if (schema.Enum != null) { var enumType = GenIntEnumType(name, schema); returnType.AdditionalMembers.Add(enumType); if (schema.HasDefaultValue()) { returnType.DefaultValue = GetEnumField(enumType, (int)(long)schema.Default); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else if (!schema.IsRequired) { returnType.CodeType = new CodeTypeReference(typeof(Nullable <>)); returnType.CodeType.TypeArguments.Add(new CodeTypeReference(enumType.Name)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } returnType.CodeType = new CodeTypeReference(enumType.Name); return(returnType); } if (schema.Default != null) { returnType.DefaultValue = new CodePrimitiveExpression((int)(long)schema.Default); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else if (!schema.IsRequired) { returnType.CodeType = new CodeTypeReference(typeof(int?)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } returnType.CodeType = new CodeTypeReference(typeof(int)); return(returnType); } if (typeRef.Name == "boolean") { if (schema.Enum != null) { throw new NotImplementedException(); } if (schema.Default != null) { returnType.DefaultValue = new CodePrimitiveExpression((bool)schema.Default); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, returnType.DefaultValue)); } else if (!schema.IsRequired) { returnType.CodeType = new CodeTypeReference(typeof(bool?)); returnType.AdditionalMembers.Add(Helpers.CreateMethodThatChecksIfTheValueOfAMemberIsNotEqualToAnotherExpression(name, new CodePrimitiveExpression(null))); return(returnType); } returnType.CodeType = new CodeTypeReference(typeof(bool)); return(returnType); } // other types: array, null throw new NotImplementedException(typeRef.Name); }