public void GetCodeTypeTest() { var schema = new JsonSchema(); var internalClassProvider = new ObjectInternalClassProvider(); schema.Type = JsonSchemaType.String; CodeTypeReference result = SchemaDecoratorUtil.GetCodeType(schema, null, internalClassProvider); Assert.AreEqual(typeof(string).FullName, result.BaseType); schema.Type = JsonSchemaType.Integer; result = SchemaDecoratorUtil.GetCodeType(schema, null, internalClassProvider); Assert.AreEqual(typeof(Nullable <>).FullName, result.BaseType); Assert.AreEqual(typeof(long).FullName, result.TypeArguments[0].BaseType); schema.Type = JsonSchemaType.Float; result = SchemaDecoratorUtil.GetCodeType(schema, null, internalClassProvider); Assert.AreEqual(typeof(Nullable <>).FullName, result.BaseType); Assert.AreEqual(typeof(double).FullName, result.TypeArguments[0].BaseType); schema.Type = JsonSchemaType.Boolean; result = SchemaDecoratorUtil.GetCodeType(schema, null, internalClassProvider); Assert.AreEqual(typeof(Nullable <>).FullName, result.BaseType); Assert.AreEqual(typeof(bool).FullName, result.TypeArguments[0].BaseType); }
internal CodeMemberProperty GenerateProperty(string name, JsonSchema propertySchema, SchemaImplementationDetails details, int index, INestedClassProvider internalClassProvider, IEnumerable <string> disallowedNames) { name.ThrowIfNullOrEmpty("name"); propertySchema.ThrowIfNull("propertySchema"); var ret = new CodeMemberProperty(); ret.Name = SchemaDecoratorUtil.GetPropertyName(name, disallowedNames); ret.Type = SchemaDecoratorUtil.GetCodeType(propertySchema, details, internalClassProvider); ret.Attributes = MemberAttributes.Public; ret.HasGet = true; var fieldReference = new CodeFieldReferenceExpression( new CodeThisReferenceExpression(), SchemaDecoratorUtil.GetFieldName(name, disallowedNames)); ret.GetStatements.Add(new CodeMethodReturnStatement(fieldReference)); ret.HasSet = true; var parameterReference = new CodeVariableReferenceExpression("value"); ret.SetStatements.Add(new CodeAssignStatement(fieldReference, parameterReference)); return(ret); }
public void GenerateAllPropertiesTest() { var implDetails = new Dictionary <JsonSchema, SchemaImplementationDetails>(); var schema = new MockSchema(); var internalClassProvider = new ObjectInternalClassProvider(); schema.SchemaDetails = new JsonSchema(); schema.SchemaDetails.Type = JsonSchemaType.Object; schema.SchemaDetails.Properties = new Dictionary <string, JsonSchema>(); implDetails.Add(schema.SchemaDetails, new SchemaImplementationDetails()); string name = "test"; foreach (var pair in StandardPropertyFieldDecoratorTest.NamesToType) { JsonSchema property = new JsonSchema(); property.Type = pair.Value; property.Id = pair.Key; property.Description = StandardPropertyFieldDecoratorTest.NamesToDescription[pair.Key]; schema.SchemaDetails.Properties.Add(pair.Key, property); implDetails.Add(property, new SchemaImplementationDetails()); } var decorator = new StandardPropertyDecorator(); IList <CodeMemberProperty> generatedProperties = decorator.GenerateAllProperties( name, schema.SchemaDetails, implDetails, internalClassProvider); Assert.NotNull(generatedProperties); Assert.AreEqual(StandardPropertyFieldDecoratorTest.NamesToType.Count, generatedProperties.Count); foreach (var field in generatedProperties) { Assert.That( StandardPropertyFieldDecoratorTest.NamesToType.ContainsKey(field.Name.ToLower()), "field name was not present in namesToType " + field.Name); } int item = 0; foreach (var pair in StandardPropertyFieldDecoratorTest.NamesToType) { var field = generatedProperties[item++]; Assert.AreEqual( pair.Key, field.Name.ToLower(), string.Format("Name different for expected at index {0}", item - 1)); Assert.AreEqual( SchemaDecoratorUtil.GetCodeType(new JsonSchema { Type = pair.Value }, null, internalClassProvider) .BaseType, field.Type.BaseType); } }
public void GetCodeTypeArgumentValidationTest() { var internalClassProvider = new ObjectInternalClassProvider(); Assert.Throws( typeof(ArgumentNullException), () => SchemaDecoratorUtil.GetCodeType(null, null, internalClassProvider)); var schema = new JsonSchema(); Assert.Throws(typeof(ArgumentNullException), () => SchemaDecoratorUtil.GetCodeType(schema, null, null)); Assert.Throws( typeof(NotSupportedException), () => SchemaDecoratorUtil.GetCodeType(schema, null, internalClassProvider)); }
internal CodeMemberField GenerateField(string name, JsonSchema propertySchema, SchemaImplementationDetails details, int index, INestedClassProvider internalClassProvider, IEnumerable <string> otherFieldNames) { name.ThrowIfNullOrEmpty("name"); propertySchema.ThrowIfNull("propertySchema"); internalClassProvider.ThrowIfNull("internalClassProvider"); details.ThrowIfNull("details"); var ret = new CodeMemberField( SchemaDecoratorUtil.GetCodeType(propertySchema, details, internalClassProvider), SchemaDecoratorUtil.GetFieldName(name, otherFieldNames)); ret.Attributes = MemberAttributes.Private; return(ret); }
public void DecorateClass(CodeTypeDeclaration typeDeclaration, ISchema schema, IDictionary <JsonSchema, SchemaImplementationDetails> implDetails, INestedClassProvider internalClassProvider) { typeDeclaration.ThrowIfNull("typeDeclaration"); schema.ThrowIfNull("schema"); implDetails.ThrowIfNull("implDetails"); internalClassProvider.ThrowIfNull("internalClassProvider"); JsonSchema details = schema.SchemaDetails; details.ThrowIfNull("schemaDetails"); // Check if this decorator can be applied to the schema); if (details.Type != JsonSchemaType.Array) { return; } if (details.Items == null || details.Items.Count != 1) { logger.Warning("Found array scheme of unhandled type. {0}", details); return; // not supported } // Generate or find the nested type JsonSchema itemScheme = details.Items[0]; SchemaImplementationDetails implDetail = implDetails[itemScheme]; implDetail.ProposedName = "Entry"; // Change the name to a custom one. CodeTypeReference item = SchemaDecoratorUtil.GetCodeType(itemScheme, implDetail, internalClassProvider); // Insert the base type before any interface declaration typeDeclaration.BaseTypes.Insert(0, new CodeTypeReference(typeof(List <>)) { TypeArguments = { item } }); }
internal void ImplementAdditionalProperties(CodeTypeDeclaration type, JsonSchema schema, IDictionary <JsonSchema, SchemaImplementationDetails> implDetails, INestedClassProvider internalClassProvider) { // Validate the input parameters. type.ThrowIfNull("type"); schema.ThrowIfNull("schema"); implDetails.ThrowIfNull("implDetails"); internalClassProvider.ThrowIfNull("internalClassProvider"); // Check if this decorator applies to the specified json schema. if (schema.AdditionalProperties == null) { return; } // Note/ToDo: Currently we only support AdditionalProperties for schemas // specifiying no normal properties, as these won't be used // by the newtonsoft json library if a dictionary is specified. if (schema.Properties != null && schema.Properties.Count > 0) { type.Comments.Add(new CodeCommentStatement("TODO: Add support for additionalProperties on schemas")); type.Comments.Add(new CodeCommentStatement(" which have normal properties defined.")); return; } // Generate the underlying type. SchemaImplementationDetails details = implDetails[schema]; CodeTypeReference underlyingTypeRef = SchemaDecoratorUtil.GetCodeType( schema.AdditionalProperties, details, internalClassProvider); // Add the base type reference. CodeTypeReference dictionaryRef = new CodeTypeReference(typeof(Dictionary <,>)); dictionaryRef.TypeArguments.Add(typeof(string)); dictionaryRef.TypeArguments.Add(underlyingTypeRef); type.BaseTypes.Add(dictionaryRef); }