private static object CreateModel(DataConversionParameters parameters) { var type = parameters.Resolver.Resolve(parameters.Schema, parameters.IsPropertyNullable, parameters.TypeNameHint); var valueGenerator = new TypeScriptValueGenerator(parameters.Resolver, parameters.Settings); var dictionaryValueType = parameters.Resolver.TryResolve(parameters.Schema.AdditionalPropertiesSchema, parameters.TypeNameHint) ?? "any"; var dictionaryValueDefaultValue = parameters.Schema.AdditionalPropertiesSchema != null ? valueGenerator.GetDefaultValue(parameters.Schema.AdditionalPropertiesSchema, parameters.Schema.AdditionalPropertiesSchema.IsNullable(parameters.Settings.SchemaType), dictionaryValueType, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues) : null; return(new { NullValue = parameters.NullValue.ToString().ToLowerInvariant(), Variable = parameters.Variable, Value = parameters.Value, HasDefaultValue = valueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues) != null, DefaultValue = valueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues), Type = type, IsNewableObject = IsNewableObject(parameters.Schema), IsDate = IsDate(parameters.Schema.Format, parameters.Settings.DateTimeType), IsDateTime = IsDateTime(parameters.Schema.Format, parameters.Settings.DateTimeType), IsDictionary = parameters.Schema.IsDictionary, DictionaryValueType = dictionaryValueType, DictionaryValueDefaultValue = dictionaryValueDefaultValue, HasDictionaryValueDefaultValue = dictionaryValueDefaultValue != null, IsDictionaryValueNewableObject = parameters.Schema.AdditionalPropertiesSchema != null && IsNewableObject(parameters.Schema.AdditionalPropertiesSchema), IsDictionaryValueDate = IsDate(parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), IsDictionaryValueDateTime = IsDateTime(parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), IsDictionaryValueNewableArray = parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true && IsNewableObject(parameters.Schema.AdditionalPropertiesSchema.Item), DictionaryValueArrayItemType = parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true? parameters.Resolver.TryResolve(parameters.Schema.AdditionalPropertiesSchema.Item, "Anonymous") ?? "any" : "any", IsArray = parameters.Schema.IsArray, ArrayItemType = parameters.Resolver.TryResolve(parameters.Schema.Item, parameters.TypeNameHint) ?? "any", IsArrayItemNewableObject = parameters.Schema.Item != null && IsNewableObject(parameters.Schema.Item), IsArrayItemDate = IsDate(parameters.Schema.Item?.Format, parameters.Settings.DateTimeType), IsArrayItemDateTime = IsDateTime(parameters.Schema.Item?.Format, parameters.Settings.DateTimeType), //StringToDateCode is used for date and date-time formats UseJsDate = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date, StringToDateCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "new Date" : (parameters.Settings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS ? "moment.parseZone" : "moment"), DateTimeToStringCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS ? "toISOString(true)" : "toISOString()", HandleReferences = parameters.Settings.HandleReferences }); }
public void When_schema_has_default_value_of_enum_it_is_generated_in_CSharp_and_TypeScript_correctly() { //// Arrange var csharpSettings = new CSharpGeneratorSettings { EnumNameGenerator = new MyEnumNameGenerator(), Namespace = "Ns" }; var csharpGenerator = new CSharpValueGenerator(csharpSettings); var csharpTypeResolver = new CSharpTypeResolver(csharpSettings); //// Act var schema = new JsonSchema4() { Type = JsonObjectType.String, Enumeration = { "Foo", "Bar" }, Default = "Bar" }; var typescriptSettings = new TypeScriptGeneratorSettings { EnumNameGenerator = new MyEnumNameGenerator() }; var typescriptGenerator = new TypeScriptValueGenerator(typescriptSettings); var typescriptTypeResolver = new TypeScriptTypeResolver(typescriptSettings); var csharpValue = csharpGenerator.GetDefaultValue(schema, true, "MyEnum", "MyEnum", true, csharpTypeResolver); var typescriptValue = typescriptGenerator.GetDefaultValue(schema, true, "MyEnum", "MyEnum", true, typescriptTypeResolver); //// Assert Assert.Equal("Ns.MyEnum.bar", csharpValue); Assert.Equal("MyEnum.bar", typescriptValue); }
public void When_schema_has_default_value_of_int_it_is_generated_in_CSharp_and_TypeScript_correctly() { //// Arrange //// Act var schema = new JsonSchema4() { Type = JsonObjectType.Integer, Default = (int)6 }; var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "int", "int", true); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "int", "int", true); //// Assert Assert.Equal("6", csharpValue); Assert.Equal("6", typescriptValue); }