public static DataServiceContextWrapper GetDefaultContext(this MockService serviceMock, OdcmModel model) { return serviceMock .GetContext() .UseJson(model.ToEdmx(), true); }
public void When_ServiceType_is_ODataV4_then_Microsoft_OData_is_Referenced() { var model = new OdcmModel(Any.ServiceMetadata(), ServiceType.ODataV4); var odmcNamespace = Any.OdcmNamespace(); model.Namespaces.Add(odmcNamespace); model.AddType(Any.ServiceOdcmClass(odmcNamespace)); var Class = Any.OdcmEntityClass(odmcNamespace); model.AddType(Class); var proxy = GetProxy(model); proxy.GetReferencedAssemblies() .Select(a => a.Name) .Distinct() .Should().Contain(new[] { "System.Runtime", "Microsoft.OData.ProxyExtensions", "System.Threading.Tasks", "System.Linq.Expressions", "Microsoft.OData.Client", "Microsoft.OData.Edm", "System.Xml.ReaderWriter", "System.Diagnostics.Debug", "System.Reflection", "System.IO", }); }
private static IEnumerable<Feature> ForOdcmClassService(OdcmServiceClass odcmClass, OdcmModel model) { return new[] { Feature.ForOdcmClassService(odcmClass, model), }; }
public IEnumerable<TextFile> GenerateProxy(OdcmModel model) { var csProject = new CSharpProject(model); var codeGenerator = new SourceCodeGenerator(model.ServiceType); return codeGenerator.Generate(csProject); }
public static Feature ForOdcmClassService(OdcmServiceClass odcmClass, OdcmModel model) { return new Feature { Classes = global::Vipr.Writer.CSharp.Classes.ForOdcmClassService(odcmClass, model), Interfaces = global::Vipr.Writer.CSharp.Interfaces.ForOdcmClassService(odcmClass), }; }
public Given_an_OdcmEnum() { _model = new OdcmModel(Any.ServiceMetadata()); _namespace = Any.EmptyOdcmNamespace(); _model.Namespaces.Add(_namespace); }
public Namespace(OdcmNamespace @namespace, OdcmModel model) { Name = NamesService.GetNamespaceName(@namespace); //Features = @namespace.Enums.SelectMany(global::Vipr.Writer.CSharp.Features.ForOdcmEnum) // .Concat(@namespace.Classes.SelectMany(global::Vipr.Writer.CSharp.Features.ForOdcmClass)) // .Concat(@namespace.Classes.SelectMany(c => global::Vipr.Writer.CSharp.Features.ForEntityContainer(c, model))); Features = @namespace.Enums.SelectMany(global::Vipr.Writer.CSharp.Features.ForOdcmEnum) .Concat(@namespace.Classes.OfType<OdcmComplexClass>().SelectMany(global::Vipr.Writer.CSharp.Features.ForOdcmClass)) .Concat(@namespace.Classes.OfType<OdcmEntityClass>().SelectMany(global::Vipr.Writer.CSharp.Features.ForOdcmClass)) .Concat(@namespace.Classes.OfType<OdcmServiceClass>().SelectMany(c => global::Vipr.Writer.CSharp.Features.ForEntityContainer(c, model))); }
public static IEnumerable<Field> ForGeneratedEdmModel(OdcmModel odcmModel) { return new[] { new Field("ParsedModel", new Type(new Identifier("global::Microsoft.OData.Edm", "IEdmModel")), "LoadModelFromString()", false, true), new ModelField("Edmx", odcmModel) }; }
public void Init(Action<OdcmModel> config = null, bool generateMocks = false) { Model = new OdcmModel(Any.ServiceMetadata()); Namespace = Any.EmptyOdcmNamespace(); Model.Namespaces.Add(Namespace); Class = Any.OdcmEntityClass(Namespace); Model.AddType(Class); OdcmContainer = Any.ServiceOdcmClass(Namespace); Model.AddType(OdcmContainer); if (config != null) config(Model); Model.ServiceMetadata["$metadata"] = Model.ToEdmx(true); Proxy = GetProxy(Model, ConfigurationProvider, generateMocks ? new[] { "DynamicProxyGenAssembly2" } : null); ConcreteType = Proxy.GetClass(Class.Namespace, Class.Name); ConcreteInterface = Proxy.GetInterface(Class.Namespace, "I" + Class.Name); FetcherType = Proxy.GetClass(Class.Namespace, Class.Name + "Fetcher"); FetcherInterface = Proxy.GetInterface(Class.Namespace, "I" + Class.Name + "Fetcher"); CollectionType = Proxy.GetClass(Class.Namespace, Class.Name + "Collection"); CollectionInterface = Proxy.GetInterface(Class.Namespace, "I" + Class.Name + "Collection"); EntityContainerType = Proxy.GetClass(Model.EntityContainer.Namespace, Model.EntityContainer.Name); EntityContainerInterface = Proxy.GetInterface(Model.EntityContainer.Namespace, "I" + Model.EntityContainer.Name); TargetEntity = new EntityArtifacts() { Class = Class, ConcreteType = ConcreteType, ConcreteInterface = ConcreteInterface, FetcherType = FetcherType, FetcherInterface = FetcherInterface, CollectionType = CollectionType, CollectionInterface = CollectionInterface }; }
public static IEnumerable<Feature> ForEntityContainer(OdcmServiceClass odcmClass, OdcmModel model) { switch (odcmClass.Kind) { case OdcmClassKind.Complex: case OdcmClassKind.MediaEntity: case OdcmClassKind.Entity: return Enumerable.Empty<Feature>(); case OdcmClassKind.Service: return Features.ForOdcmClassService((OdcmServiceClass)odcmClass, model); } throw new NotImplementedException(string.Format("OdcmClassKind {0} is not recognized", odcmClass.Kind)); }
public Given_an_OdcmClass_Complex() { _model = new OdcmModel(Any.ServiceMetadata()); _namespace = Any.EmptyOdcmNamespace(); _model.Namespaces.Add(_namespace); _class = Any.OdcmComplexClass(e => e.Namespace = _namespace); _model.AddType(_class); _proxy = GetProxy(_model); _classType = _proxy.GetClass(_class.Namespace, _class.Name); }
public Given_an_OdcmParameter() { _model = new OdcmModel(Any.ServiceMetadata()); _namespace = Any.EmptyOdcmNamespace(); _model.Namespaces.Add(_namespace); _class = Any.OdcmEntityClass(_namespace); _model.AddType(_class); _method = Any.OdcmMethod(m => m.Parameters.Clear()); _class.Methods.Add(_method); _param = Any.OdcmParameter(); _method.Parameters.Add(_param); _expectedMethodName = _method.Name + "Async"; }
public Given_a_nullable_Primitive_OdcmProperty() { _model = new OdcmModel(Any.ServiceMetadata()); _namespace = Any.EmptyOdcmNamespace(); _model.Namespaces.Add(_namespace); _class = Any.OdcmEntityClass(_namespace); _property = Any.PrimitiveOdcmProperty(p => { p.IsNullable = true; p.Class = _class; }); _class.Properties.Add(_property); _model.AddType(_class); }
public static IEnumerable <OdcmClass> GetMediaEntityTypes(this OdcmModel model) { var namespaces = GetOdcmNamespaces(model); return(namespaces.SelectMany(@namespace => @namespace.Classes.Where(x => x.IsStreamedEntity()))); }
public Given_a_ConfigurationProvider() { _model = Any.OdcmModel(); }
public OdcmModel GenerateOdcmModel(IEnumerable<TextFile> serviceMetadata) { if (serviceMetadata == null) throw new ArgumentNullException("serviceMetadata"); var edmxFile = serviceMetadata.FirstOrDefault(f => f.RelativePath == MetadataKey); if (edmxFile == null) throw new ArgumentException( String.Format("Argument must contain file with RelateivePath \"{0}", MetadataKey), "serviceMetadata"); var edmx = XElement.Parse(edmxFile.Contents); IEnumerable<EdmError> errors; if (!EdmxReader.TryParse(edmx.CreateReader(ReaderOptions.None), out _edmModel, out errors)) { Debug.Assert(errors != null, "errors != null"); if (errors.FirstOrDefault() == null) { throw new InvalidOperationException(); } throw new InvalidOperationException(errors.FirstOrDefault().ErrorMessage); } _odcmModel = new OdcmModel(serviceMetadata); AddPrimitives(); WriteNamespaces(); return _odcmModel; }
public string GetProxyXmlDocumentContent(OdcmModel model, IConfigurationProvider configurationProvider = null, IEnumerable<string> internalsVisibleTo = null) { var proxySources = GetProxySources(model, configurationProvider, internalsVisibleTo); return GetXmlDocumentContent(ReferencedAssemblies, proxySources.Select(f => f.Contents).ToArray()); }
public CSharpProject(OdcmModel model) { Namespaces = global::Vipr.Writer.CSharp.Namespaces.ForModel(model); }
protected static CustomT4Host Host(ITemplateInfo templateInfo, string templatesDirectory, OdcmObject odcmObject, OdcmModel odcmModel) { if (_host == null) { _host = new CustomT4Host(templateInfo, templatesDirectory, odcmObject, odcmModel); } else { _host.Reset(templateInfo, templatesDirectory, odcmObject, odcmModel); } return(_host); }
private void ExportOcdmModel(OdcmModel model) { if (string.IsNullOrEmpty(_ocdmModelExportPath)) return; var jss = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }; File.WriteAllText(_ocdmModelExportPath, JsonConvert.SerializeObject(model, jss)); }
public Given_an_OdcmNamespace() { _model = new OdcmModel(Any.ServiceMetadata()); }
internal static IEnumerable<Namespace> ForModel(OdcmModel model) { return model.Namespaces.Where(n => !n.Name.Equals("edm", StringComparison.OrdinalIgnoreCase)) .Select(n => new Namespace(n, model)); }
public CodeWriterJava(OdcmModel model) : base(model) { }
protected static CustomT4Host Host(ITemplateInfo templateInfo, string templatesDirectory, OdcmObject odcmObject, OdcmModel odcmModel) { // Need to always set the host. Typically, this is run against a single platform when generating codefiels. // In test cases, we need to target multiple platforms. Since much of this code is static, we need to make sure // reset the information provided to the template processor. This change fixes a bug when targeting // multiple platforms in a test. _host = new CustomT4Host(templateInfo, templatesDirectory, odcmObject, odcmModel); return(_host); }
// IOdcmWriter public IEnumerable <TextFile> GenerateProxy(OdcmModel model) { this.CurrentModel = model; logger.Debug("Currently processing model {0}", model); return(this.ProcessTemplates()); }
public static IEnumerable <OdcmProperty> GetProperties(this OdcmModel model) { return (model.Namespaces.SelectMany(n => n.Classes.SelectMany(c => c.Properties))); }
public static IEnumerable <OdcmProperty> GetStreamProperties(this OdcmModel model) { return(model.GetProperties(typeName: "Stream", longDescriptionMatches: null)); }
private void ConfigureReaderMock(string metadata, OdcmModel model) { _readerMock .Setup( r => r.GenerateOdcmModel( It.Is<IEnumerable<TextFile>>( g => g.Any(f => f.RelativePath.Equals("$metadata") && f.Contents.Equals(metadata))))) .Returns(model); }
public static IEnumerable <OdcmClass> GetEntityTypes(this OdcmModel model) { var namespaces = GetOdcmNamespaces(model); return(namespaces.SelectMany(@namespace => @namespace.Classes.Where(x => x is OdcmEntityClass || x is OdcmMediaClass))); }
public Given_an_OdcmObject_with_Description() { _model = new OdcmModel(Any.ServiceMetadata()); _namespace = Any.EmptyOdcmNamespace(); _model.Namespaces.Add(_namespace); }
public static IEnumerable <OdcmClass> GetComplexTypes(this OdcmModel model) { var namespaces = GetOdcmNamespaces(model); return(namespaces.SelectMany(@namespace => @namespace.Classes.Where(x => x is OdcmComplexClass && x.CanonicalName().ToLowerInvariant() != "microsoft.graph.json"))); }
public static IEnumerable <OdcmMethod> GetMethods(this OdcmModel model) { return(model.GetEntityTypes().SelectMany(entityType => entityType.Methods)); }
/// <summary> /// This method takes a specific property and determines whether its could be referenced in the navigation property of another type. /// The navigation property must also come from chain where the containTarget=true from the root Service class to prove that we can reference it. /// Ideally we should call this function if GetServiceCollectionNavigationPropertyForPropertyType returns null /// </summary> /// <param name="odcmProperty">The property to check</param> /// <param name="model">The model to use</param> /// <returns></returns> public static bool IsPropertyChainedToContainedServiceNavigationProperty(this OdcmProperty odcmProperty, OdcmModel model) { // keep track of the types we've traversed to avoid circular references var navigatedTypes = new HashSet <string>(); var matchingRootProperty = GetOdcmNamespaces(model) .SelectMany( @namespace => @namespace .Classes .FirstOrDefault(odcmClass => odcmClass.Kind == OdcmClassKind.Service) // find the root service class ?.Properties ?.Where(serviceProperty => serviceProperty.GetType() == typeof(OdcmSingleton)) // find the singleton properties ?.Where(singleton => singleton. Type.AsOdcmClass() ?.Properties ?.Where(property => property.ContainsTarget) // find singleton type properties that are self contained ?.Any(property => property.IsPropertyTypeChainedContainedNavigationProperty( // keep following the containsTarget=True to find if we can use a reference odcmProperty, navigatedTypes, property.IsCollection ? $"{singleton.Name}/{property.Name}/{{id}}" : $"{singleton.Name}/{property.Name}") ) ?? false ) ?? Array.Empty <OdcmProperty>() ).FirstOrDefault(); return(matchingRootProperty != null); }
public static IEnumerable <OdcmEnum> GetEnumTypes(this OdcmModel model) { var namespaces = GetOdcmNamespaces(model); return(namespaces.SelectMany(@namespace => @namespace.Types.OfType <OdcmEnum>())); }
public string GetProxyXmlDocumentContent(OdcmModel model, IConfigurationProvider configurationProvider = null, IEnumerable <string> internalsVisibleTo = null) { var proxySources = GetProxySources(model, configurationProvider, internalsVisibleTo); return(GetXmlDocumentContent(ReferencedAssemblies, proxySources.Select(f => f.Contents).ToArray())); }
public IEnumerable <TextFile> GenerateProxy(OdcmModel model) { return(Enumerable.Empty <TextFile>()); }
/// <summary> /// Get the service collection navigation property for the given property type /// /// We have revised the LINQ statement to support the following OData rules /// regarding containment: /// /// 1. If a navigation property specifies "ContainsTarget='true'", it is self-contained. /// Generate a direct path to the item (ie "parent/child"). /// 2. If a navigation property does not specify ContainsTarget but there is a defined EntitySet /// of the given type, it is a reference relationship. Generate a reference path to the item (ie "item/$ref"). /// 3. If a navigation property does not have a defined EntitySet but there is a Singleton which has /// a self-contained reference to the given type, we can make a relationship to the implied EntitySet of /// the singleton. Generate a reference path to the item (ie "singleton/item/$ref"). /// 4. If none of the above pertain. Try to call <see cref="IsPropertyChainedToContainedServiceNavigationProperty"/> to ascertain if this is a metadata error as /// the relationship could be more than one level deep. (The nav property we are looking for could be a navigation property of another navigation property :) ). /// 5. If none of the above pertain to the navigation property, it should be treated as a metadata error. /// </summary> public static OdcmProperty GetServiceCollectionNavigationPropertyForPropertyType(this OdcmProperty odcmProperty, OdcmModel model) { // Try to find the first collection navigation property for the specified type directly on the service // class object. If an entity is used in a reference property a navigation collection // on the client for that type is required. try { var explicitProperty = GetOdcmNamespaces(model) .SelectMany( @namespace => @namespace .Classes .Where(odcmClass => odcmClass.Kind == OdcmClassKind.Service) .SelectMany(service => (service as OdcmServiceClass).NavigationProperties()) .Where(property => property.IsCollection && property.Projection.Type.FullName.Equals(odcmProperty.Projection.Type.FullName)) ).FirstOrDefault(); if (explicitProperty != null) { return(explicitProperty); } // Check the singletons for a matching implicit EntitySet else { var implicitProperty = GetOdcmNamespaces(model) .SelectMany( @namespace => @namespace .Classes .Where(odcmClass => odcmClass.Kind == OdcmClassKind.Service) .FirstOrDefault() ?.Properties ?.Where(property => property.GetType() == typeof(OdcmSingleton)) //Get the list of singletons defined by the service ?.Where(singleton => singleton .Type .AsOdcmClass() ?.Properties //Find navigation properties on the singleton that are self-contained (implicit EntitySets) that match the type //we are searching for .Where(prop => prop.ContainsTarget == true && prop.Type.Name == odcmProperty.Type.Name) .FirstOrDefault() != null ) ?? new OdcmProperty[] { } ).FirstOrDefault(); if (implicitProperty != null) { return(implicitProperty); } } //If we are unable to find a valid EntitySet for the property, treat this //as an exception so the service has an opportunity to correct this in the metadata throw new Exception("Found no valid EntitySet for the given property."); } catch (Exception e) { logger.Error("The navigation property \"{0}\" on class \"{1}\" does not specify it is self-contained nor is it defined in an explicit or implicit EntitySet", odcmProperty.Name.ToString(), odcmProperty.Class.FullName.ToString()); logger.Error(e); return(null); } }
public CodeWriterPHP(OdcmModel model) : base(model) { }
private void ConfigureMocks(string metadata, OdcmModel model, IEnumerable<TextFile> outputTextFiles) { ConfigureReaderMock(metadata, model); ConfigureWriterMock(model, outputTextFiles); ConfigureBootstrapperMock(); }
private void ConfigureWriterMock(OdcmModel model, IEnumerable<TextFile> outputTextFiles) { _writerMock .Setup(w => w.GenerateProxy(It.Is<OdcmModel>(m => m == model))) .Returns(outputTextFiles); }
public IEnumerable<TextFile> GenerateProxy(OdcmModel model) { return Enumerable.Empty<TextFile>(); }
public static string GetEntityContainer(this OdcmModel model) { return(model.EntityContainer.Name); }
public static OdcmModel OdcmModel(Action<OdcmModel> config = null) { var retVal = new OdcmModel(Any.ServiceMetadata()); retVal.Namespaces.AddRange(Any.Sequence(s => Any.OdcmNamespace())); var containerNamespace = retVal.Namespaces.RandomElement(); containerNamespace.Types.Add(Any.ServiceOdcmClass(containerNamespace)); if (config != null) config(retVal); return retVal; }
public static IEnumerable<Class> ForOdcmClassService(OdcmServiceClass odcmClass, OdcmModel model) { return new[] {Class.ForEntityContainer(model, odcmClass)}; }
public IEnumerable <TextFile> GenerateProxy(OdcmModel model) { return(ProcessTemplates(model)); }
public ModelField(string name, OdcmModel odcmModel) : base(name, new Type(new Identifier("System", "String")), string.Format("@\"{0}\"", ModelToEscapedString(odcmModel.ServiceMetadata["$metadata"])), true) { }
internal static Class ForEntityContainer(OdcmModel odcmModel, OdcmServiceClass odcmContainer) { return new Class { AccessModifier = "public ", Constructors = global::Vipr.Writer.CSharp.Constructors.ForEntityContainer(odcmContainer), Description = odcmContainer.Description, Fields = global::Vipr.Writer.CSharp.Fields.ForEntityContainer(odcmContainer), Interfaces = global::Vipr.Writer.CSharp.ImplementedInterfaces.ForEntityContainer(odcmContainer), Identifier = NamesService.GetEntityContainerTypeName(odcmContainer), NestedClasses = new[] { ForGeneratedEdmModel(odcmModel) }, Methods = global::Vipr.Writer.CSharp.Methods.ForEntityContainer(odcmContainer), Properties = global::Vipr.Writer.CSharp.Properties.ForEntityContainer(odcmContainer) }; }
internal static Class ForGeneratedEdmModel(OdcmModel odcmModel) { return new Class { AbstractModifier = "abstract ", AccessModifier = "private ", Fields = global::Vipr.Writer.CSharp.Fields.ForGeneratedEdmModel(odcmModel), Identifier = new Identifier("", "GeneratedEdmModel"), Methods = global::Vipr.Writer.CSharp.Methods.ForGeneratedEdmModel(), }; }