Ejemplo n.º 1
0
        /// <summary>
        /// Generate CodeDom of the client API for ApiDescriptions.
        /// </summary>
        /// <param name="descriptions">Web Api descriptions exposed by Configuration.Services.GetApiExplorer().ApiDescriptions</param>
        public void CreateCodeDom(OpenApiPaths paths, OpenApiComponents components)
        {
            if (paths == null && components == null)
            {
                return;
            }

            clientNamespace = new CodeNamespace(settings.ClientNamespace);
            CodeCompileUnit.Namespaces.Add(clientNamespace);            //namespace added to Dom

            ComponentsToTsTypes componentsToTsTypes = new ComponentsToTsTypes(settings, CodeCompileUnit, clientNamespace);

            componentsToTsTypes.CreateCodeDom(components);

            if (paths == null)
            {
                return;
            }

            AddBasicReferences();

            string[] containerClassNames = GetContainerClassNames(paths);

            CodeTypeDeclaration[] newClassesCreated = containerClassNames.Select(d => CreateControllerClientClass(clientNamespace, d)).ToArray();

            foreach (KeyValuePair <string, OpenApiPathItem> p in paths)
            {
                string relativePath = p.Key;
                foreach (KeyValuePair <OperationType, OpenApiOperation> op in p.Value.Operations)
                {
                    ClientApiTsFunctionGenAbstract apiFunctionGen = apiFunctionGenFactory();
                    CodeMemberMethod apiFunction = apiFunctionGen.CreateApiFunction(settings, relativePath, op.Key, op.Value, componentsToTsTypes);
                    if (apiFunction == null)
                    {
                        System.Diagnostics.Trace.TraceWarning($"Not to generate TS for {p.Key} {op.Key}.");
                        continue;
                    }

                    string containerClassName         = nameComposer.GetContainerName(op.Value, p.Key);
                    CodeTypeDeclaration existingClass = LookupExistingClass(containerClassName);
                    existingClass.Members.Add(apiFunction);
                }
            }


            foreach (CodeTypeDeclaration c in newClassesCreated)
            {
                AddHelperFunctionsInClass(c);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate CodeDom of the client API for ApiDescriptions.
        /// </summary>
        /// <param name="descriptions">Web Api descriptions exposed by Configuration.Services.GetApiExplorer().ApiDescriptions</param>
        public void CreateCodeDom(WebApiDescription[] descriptions)
        {
            if (descriptions == null)
            {
                throw new ArgumentNullException("descriptions");
            }

            AddBasicReferences();

            GenerateTsFromPoco();

            //controllers of ApiDescriptions (functions) grouped by namespace
            var controllersGroupByNamespace = descriptions.Select(d => d.ActionDescriptor.ControllerDescriptor)
                                              .Distinct()
                                              .GroupBy(d => d.ControllerType.Namespace)
                                              .OrderBy(k => k.Key);// order by namespace

            //Create client classes mapping to controller classes
            CodeTypeDeclaration[] newControllerClassesCreated = null;
            foreach (var grouppedControllerDescriptions in controllersGroupByNamespace)
            {
                var clientNamespaceText = (grouppedControllerDescriptions.Key + jsOutput.ClientNamespaceSuffix).Replace('.', '_');
                var clientNamespace     = new CodeNamespace(clientNamespaceText);

                TargetUnit.Namespaces.Add(clientNamespace);                //namespace added to Dom

                newControllerClassesCreated = grouppedControllerDescriptions
                                              .OrderBy(d => d.ControllerName)
                                              .Select(d =>
                {
                    var controllerFullName = d.ControllerType.Namespace + "." + d.ControllerName;
                    if (apiSelections.ExcludedControllerNames != null && apiSelections.ExcludedControllerNames.Contains(controllerFullName))
                    {
                        return(null);
                    }

                    return(CreateControllerClientClass(clientNamespace, d.ControllerName));
                }).Where(d => d != null).ToArray();                        //add classes into the namespace
            }

            foreach (var d in descriptions)
            {
                var controllerNamespace = d.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace;
                var controllerName      = d.ActionDescriptor.ControllerDescriptor.ControllerName;
                var controllerFullName  = controllerNamespace + "." + controllerName;
                if (apiSelections.ExcludedControllerNames != null && apiSelections.ExcludedControllerNames.Contains(controllerFullName))
                {
                    continue;
                }

                var existingClientClass = LookupExistingClassInCodeDom(controllerNamespace, controllerName);
                System.Diagnostics.Trace.Assert(existingClientClass != null);

                var apiFunction = apiFunctionGen.CreateApiFunction(d, poco2TsGen, this.jsOutput.StringAsString);
                existingClientClass.Members.Add(apiFunction);
            }

            RefineOverloadingFunctions();

            foreach (var c in newControllerClassesCreated)
            {
                AddHelperFunctionsInClass(c);
            }
        }