public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings, CSharpTypeResolver resolver, JsonSchema4 schema, IEnumerable<PropertyModel> properties)
        {
            _resolver = resolver;
            _schema = schema;
            _settings = settings;

            Class = typeName;
            Properties = properties;
        }
Beispiel #2
0
        internal PropertyModel(JsonProperty property, CSharpTypeResolver resolver, CSharpGeneratorSettings settings)
            : base(property, new DefaultValueGenerator(resolver))
        {
            _property = property;
            _settings = settings;
            _resolver = resolver;

            PropertyName = ConversionUtilities.ConvertToUpperCamelCase(GetGeneratedPropertyName(), true);
        }
 /// <summary>Initializes a new instance of the <see cref="CSharpTypeResolver"/> class.</summary>
 /// <param name="settings">The generator settings.</param>
 /// <param name="rootObject">The root object to search for JSON Schemas.</param>
 public CustomCSharpTypeResolver(CSharpGeneratorSettings settings, object rootObject)
     : base(settings, rootObject)
 {
 }
Beispiel #4
0
 /// <summary>Initializes a new instance of the <see cref="CSharpTypeResolver" /> class.</summary>
 /// <param name="definition">The definition.</param>
 /// <param name="settings">The generator settings.</param>
 public SwaggerToCSharpTypeResolver(CSharpGeneratorSettings settings, IDictionary <string, JsonSchema4> definition)
     : base(settings, definition.Where(p => p.Key != "Exception").Select(p => p.Value).ToArray())
 {
     _exceptionSchema = definition.ContainsKey("Exception") ? definition["Exception"] : null;
 }
Beispiel #5
0
 public JsonSchemaToCSharpCommand()
 {
     Settings = new CSharpGeneratorSettings();
 }
Beispiel #6
0
 /// <summary>Initializes a new instance of the <see cref="PropertyModel"/> class.</summary>
 /// <param name="classTemplateModel">The class template model.</param>
 /// <param name="property">The property.</param>
 /// <param name="resolver">The resolver.</param>
 /// <param name="settings">The settings.</param>
 public PropertyModel(ClassTemplateModel classTemplateModel, JsonProperty property, CSharpTypeResolver resolver, CSharpGeneratorSettings settings)
     : base(classTemplateModel, property, new CSharpDefaultValueGenerator(resolver, settings.NullHandling), settings)
 {
     _property = property;
     _settings = settings;
     _resolver = resolver;
 }
 /// <summary>The DateFormatConverterTemplateModel.</summary>
 public DateFormatConverterTemplateModel(CSharpGeneratorSettings settings)
 {
     _settings = settings;
 }
Beispiel #8
0
 public DotvmmCSharpTypeNameGenerator(CSharpGeneratorSettings settings)
 {
     this.settings = settings;
 }
Beispiel #9
0
 //This will handle correct type serialization for integers
 public CustomResolver(CSharpGeneratorSettings settings) : base(settings)
 {
 }
 /// <summary>Initializes a new instance of the <see cref="CSharpDefaultValueGenerator" /> class.</summary>
 /// <param name="typeResolver">The type resolver.</param>
 /// <param name="settings">The settings.</param>
 public CSharpDefaultValueGenerator(ITypeResolver typeResolver, CSharpGeneratorSettings settings) : base(typeResolver)
 {
     _settings = settings;
 }
Beispiel #11
0
        public async Task When_enum_is_integer_flags_it_should_use_declared_values()
        {
            //// Arrange
            var json = @"
{
    ""properties"": {
        ""foo"": {
            ""$ref"": ""#/definitions/FlagsTestEnum""
        }
    },
    ""definitions"": {
       ""FlagsTestEnum"": {
           ""type"": ""integer"",
           ""description"": """",
           ""x-enumFlags"": true,
           ""x-enumNames"": [
             ""None"",
             ""FirstBit"",
             ""SecondBit"",
             ""ThirdBit"",
             ""FirstAndSecondBits"",
             ""All""
           ],
        ""enum"": [
          0,
          1,
          2,
          4,
          3,
          7
        ]
      }
    }
}";
            //// Act
            var schema = await JsonSchema.FromJsonAsync(json);

            var settings  = new CSharpGeneratorSettings();
            var generator = new CSharpGenerator(schema, settings);

            var code = generator.GenerateFile("Foo");

            //// Assert
            Assert.DoesNotContain("public enum Anonymous", code);
            // Verify previous incorrect logic wasn't used to determine enum values (and doesn't generate duplicate incorrect values):
            Assert.DoesNotContain("None = 1,", code);
            Assert.DoesNotContain("FirstBit = 2,", code);
            Assert.DoesNotContain("SecondBit = 4,", code);
            Assert.DoesNotContain("ThirdBit = 8,", code);
            Assert.DoesNotContain("FirstAndSecondBits = 16,", code);
            Assert.DoesNotContain("All = 16,", code);
            Assert.DoesNotContain("All = 32,", code);
            // Verify correct logic:
            Assert.Contains("public enum FlagsTestEnum", code);
            Assert.Contains("None = 0,", code);
            Assert.Contains("FirstBit = 1,", code);
            Assert.Contains("SecondBit = 2,", code);
            Assert.Contains("ThirdBit = 4,", code);
            Assert.Contains("FirstAndSecondBits = 3,", code);
            Assert.Contains("All = 7,", code);
        }
Beispiel #12
0
 public RefitCSharpTypeResolver(CSharpGeneratorSettings settings) : base(settings)
 {
 }
Beispiel #13
0
 public RefitCSharpTypeResolver(CSharpGeneratorSettings settings, JsonSchema exceptionSchema) : base(settings, exceptionSchema)
 {
 }
Beispiel #14
0
        public static void WriteCSharp(string sampleJson, FileInfo outputCSharp, CSharpGeneratorSettings settings)
        {
            JsonSchema4 schema = JsonSchema4.FromSampleJson(sampleJson);

            WriteCSharp(schema, outputCSharp, settings);
        }
 /// <summary>The JsonInheritanceConverterTemplateModel.</summary>
 public JsonInheritanceConverterTemplateModel(CSharpGeneratorSettings settings)
 {
     _settings = settings;
 }
 /// <summary>Initializes a new instance of the <see cref="CSharpTypeResolver" /> class.</summary>
 /// <param name="settings">The generator settings.</param>
 /// <param name="exceptionSchema">The exception type schema.</param>
 /// <param name="document">The document </param>
 public SwaggerToCSharpTypeResolver(CSharpGeneratorSettings settings, JsonSchema4 exceptionSchema)
     : base(settings)
 {
     ExceptionSchema = exceptionSchema;
 }
Beispiel #17
0
 public CustomResolver(CSharpGeneratorSettings settings, JsonSchema exceptionSchema) : base(settings, exceptionSchema)
 {
 }
Beispiel #18
0
 /// <summary>Initializes a new instance of the <see cref="SwaggerToCSharpGeneratorSettings"/> class.</summary>
 public SwaggerToCSharpGeneratorSettings()
 {
     ClassName = "{controller}Client";
     AdditionalNamespaceUsages = null;
     CSharpGeneratorSettings   = new CSharpGeneratorSettings();
 }
Beispiel #19
0
 internal PropertyModel(JsonProperty property, CSharpTypeResolver resolver, CSharpGeneratorSettings settings) : base(property)
 {
     _property = property;
     _settings = settings;
     _resolver = resolver;
 }
 internal SwaggerToCSharpGeneratorBase(SwaggerService service, CSharpGeneratorSettings settings)
 {
     Resolver = new SwaggerToCSharpTypeResolver(settings, service.Definitions);
 }
Beispiel #21
0
 public void SetGeneratorSettings(CSharpGeneratorSettings settings)
 {
 }
Beispiel #22
0
 /// <summary>Initializes a new instance of the <see cref="EnumTemplateModel" /> class.</summary>
 /// <param name="typeName">Name of the type.</param>
 /// <param name="schema">The schema.</param>
 /// <param name="settings">The settings.</param>
 public EnumTemplateModel(string typeName, JsonSchema schema, CSharpGeneratorSettings settings)
 {
     _schema   = schema;
     _settings = settings;
     Name      = typeName;
 }
        public async Task CreateClient(TextWriter writer)
        {
            var interfacesToWrite = new InterfaceManager();

            writer.WriteLine(
                $@"using Threax.AspNetCore.Halcyon.Client;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Net.Http;
using System.Linq;

namespace {options.Namespace} {{"
                );

            await WriteClient(interfacesToWrite, writer);

            writer.WriteLine("}");

            //Write interfaces, kind of weird, no good docs for this
            var settings = new CSharpGeneratorSettings()
            {
                Namespace = options.Namespace,
                GenerateDataAnnotations = false,
                ClassStyle = CSharpClassStyle.Poco,
                RequiredPropertiesMustBeDefined = false,
                EnumNameGenerator = new EnumValueEnumNameGenerator(),
                ArrayType         = "List" //This is imported in the using statements above (System.Collections.Generic.List)
            };

            //Gather up everything to write, skip duplicate instances of the same thing
            Dictionary <String, CodeArtifact> codeArtifacts = new Dictionary <String, CodeArtifact>();

            foreach (var item in interfacesToWrite.Interfaces)
            {
                //Remove any properties from item that are hal embeds
                var propertiesToRemove = item.Value.Properties.Where(i => i.Value.IsHalEmbedded()).ToList();
                foreach (var remove in propertiesToRemove)
                {
                    item.Value.Properties.Remove(remove.Key);
                }

                var resolver = new CSharpTypeResolver(settings);
                resolver.RegisterSchemaDefinitions(new Dictionary <String, JsonSchema4>()
                {
                    { item.Key, item.Value }
                });                                                                                                     //Add all discovered generators

                var generator = new CSharpGenerator(item.Value, settings, resolver);
                var artifacts = generator.GenerateTypes();
                foreach (var artifact in artifacts.Artifacts)
                {
                    if (!codeArtifacts.ContainsKey(artifact.TypeName))
                    {
                        codeArtifacts.Add(artifact.TypeName, artifact);
                    }
                }
            }

            //Write the classes officially
            //From TypeScriptGenerator.cs GenerateFile, (NJsonSchema 9.10.49)
            var model = new FileTemplateModel()
            {
                Namespace = settings.Namespace ?? string.Empty,
                TypesCode = ConversionUtilities.TrimWhiteSpaces(string.Join("\n\n", CodeArtifactCollection.OrderByBaseDependency(codeArtifacts.Values).Select(p => p.Code))),
            };

            var template = settings.TemplateFactory.CreateTemplate("CSharp", "File", model);
            var classes  = ConversionUtilities.TrimWhiteSpaces(template.Render());

            writer.WriteLine(classes);
            //End Write Interfaces
        }
Beispiel #24
0
        public async Task When_oneOf_has_multiple_refs_then_the_properties_should_expand_to_single_class()
        {
            //// Arrange
            var json = @"{
                '$schema': 'http://json-schema.org/draft-04/schema#',
                'id': 'http://some.domain.com/foo.json',
                'type': 'object',
                'additionalProperties': false,
                'definitions': {
                    'tRef1': {
                        'properties': {
                            'val1': {
                                'type': 'string',
                            }
                        }
                    },
                    'tRef2': {
                        'properties': {
                            'val2': {
                                'type': 'string',
                            }
                        }
                    },
                    'tRef3': {
                        'properties': {
                            'val3': {
                                'type': 'string',
                            }
                        }
                    }
                },
                'properties' : {
                    'tAgg': {
                        'oneOf': [
                            {'$ref': '#/definitions/tRef1'},
                            {'$ref': '#/definitions/tRef2'},
                            {'$ref': '#/definitions/tRef3'}
                        ]
                    }
                }
            }";

            //// Act
            var schema = await JsonSchema.FromJsonAsync(json);

            var settings = new CSharpGeneratorSettings {
                ClassStyle = CSharpClassStyle.Poco, Namespace = "ns"
            };
            var generator = new CSharpGenerator(schema, settings);
            var code      = generator.GenerateFile("Foo");

            //// Assert
            Assert.Contains(@"public partial class TRef1 
    {
        [Newtonsoft.Json.JsonProperty(""val1"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Val1 { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }".Replace("\r", string.Empty), code);
            Assert.Contains(@"public partial class TRef1 
    {
        [Newtonsoft.Json.JsonProperty(""val1"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Val1 { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }".Replace("\r", string.Empty), code);

            Assert.Contains(@"public partial class TRef2 
    {
        [Newtonsoft.Json.JsonProperty(""val2"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Val2 { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }".Replace("\r", string.Empty), code);

            Assert.Contains(@"public partial class TRef3 
    {
        [Newtonsoft.Json.JsonProperty(""val3"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Val3 { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }".Replace("\r", string.Empty), code);

            Assert.Contains(@"public partial class Foo 
    {
        [Newtonsoft.Json.JsonProperty(""tAgg"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public TAgg TAgg { get; set; }".Replace("\r", string.Empty), code);

            Assert.Contains(@"public partial class TAgg 
    {
        [Newtonsoft.Json.JsonProperty(""val1"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Val1 { get; set; }
    
        [Newtonsoft.Json.JsonProperty(""val2"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Val2 { get; set; }
    
        [Newtonsoft.Json.JsonProperty(""val3"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Val3 { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }".Replace("\r", string.Empty), code);
        }
Beispiel #25
0
 /// <summary>Initializes a new instance of the <see cref="PropertyModel"/> class.</summary>
 /// <param name="classTemplateModel">The class template model.</param>
 /// <param name="property">The property.</param>
 /// <param name="resolver">The resolver.</param>
 /// <param name="settings">The settings.</param>
 public PropertyModel(ClassTemplateModel classTemplateModel, JsonProperty property, CSharpTypeResolver resolver, CSharpGeneratorSettings settings)
     : base(property, classTemplateModel, new CSharpValueGenerator(resolver, settings), settings)
 {
     _property = property;
     _settings = settings;
     _resolver = resolver;
 }