/// <summary>Generates the parameter name for the given parameter.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <param name="allParameters">All parameters.</param>
        /// <returns>The parameter name.</returns>
        public string Generate(OpenApiParameter parameter, IEnumerable <OpenApiParameter> allParameters)
        {
            var name = !string.IsNullOrEmpty(parameter.OriginalName) ?
                       parameter.OriginalName : parameter.Name;

            if (string.IsNullOrEmpty(name))
            {
                return("unnamed");
            }

            var variableName = ConversionUtilities.ConvertToLowerCamelCase(name
                                                                           .Replace("-", "_")
                                                                           .Replace(".", "_")
                                                                           .Replace("$", string.Empty)
                                                                           .Replace("@", string.Empty)
                                                                           .Replace("[", string.Empty)
                                                                           .Replace("]", string.Empty), true);

            if (allParameters.Count(p => p.Name == name) > 1)
            {
                return(variableName + parameter.Kind);
            }

            return(variableName);
        }
예제 #2
0
 internal virtual string GetParameterVariableName(SwaggerParameter parameter)
 {
     return(ConversionUtilities.ConvertToLowerCamelCase(parameter.Name
                                                        .Replace("-", "_")
                                                        .Replace(".", "_")
                                                        .Replace("$", string.Empty), true));
 }
예제 #3
0
        /// <summary>Generates the parameter name for the given parameter.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <param name="allParameters">All parameters.</param>
        /// <returns>The parameter name.</returns>
        public string Generate(SwaggerParameter parameter, IEnumerable <SwaggerParameter> allParameters)
        {
            if (string.IsNullOrEmpty(parameter.Name))
            {
                return("unnamed");
            }

            var variableName = ConversionUtilities.ConvertToLowerCamelCase(parameter.Name
                                                                           .Replace("-", "_")
                                                                           .Replace(".", "_")
                                                                           .Replace("$", string.Empty)
                                                                           .Replace("[", string.Empty)
                                                                           .Replace("]", string.Empty), true);

            if (allParameters.Count(p => p.Name == parameter.Name) > 1)
            {
                variableName = variableName + parameter.Kind;
            }

            var key = $"{parameter.ParentOperation?.OperationId}.{variableName}";

            if (!_knowParameters.ContainsKey(key))
            {
                _knowParameters.Add(key, 1);
            }
            else
            {
                variableName = $"{variableName}{_knowParameters[key]++}";
            }

            return(variableName);
        }
예제 #4
0
 /// <summary>Generates the property name.</summary>
 /// <param name="property">The property.</param>
 /// <returns>The new name.</returns>
 public virtual string Generate(JsonProperty property)
 {
     return(ConversionUtilities.ConvertToLowerCamelCase(property.Name
                                                        .Replace("@", "")
                                                        .Replace(".", "-"), true)
            .Replace("-", "_"));
 }
예제 #5
0
        internal List <OperationModel> GetOperations <TGenerator>(SwaggerService service, TypeResolverBase <TGenerator> resolver)
            where TGenerator : TypeGeneratorBase
        {
            service.GenerateOperationIds();

            var operations = service.Paths
                             .SelectMany(pair => pair.Value.Select(p => new { Path = pair.Key.Trim('/'), HttpMethod = p.Key, Operation = p.Value }))
                             .Select(tuple =>
            {
                var operation = tuple.Operation;
                var responses = operation.Responses.Select(response => new ResponseModel(response, this)).ToList();

                var defaultResponse = responses.SingleOrDefault(r => r.StatusCode == "default");
                if (defaultResponse != null)
                {
                    responses.Remove(defaultResponse);
                }

                return(new OperationModel
                {
                    Path = tuple.Path,
                    HttpMethod = tuple.HttpMethod,
                    Operation = tuple.Operation,
                    OperationName = BaseSettings.OperationNameGenerator.GetOperationName(service, tuple.Path, tuple.HttpMethod, tuple.Operation),

                    ResultType = GetResultType(operation),
                    HasResultType = HasResultType(operation),
                    ResultDescription = GetResultDescription(operation),

                    ExceptionType = GetExceptionType(operation),
                    HasFormParameters = operation.Parameters.Any(p => p.Kind == SwaggerParameterKind.FormData),
                    Responses = responses,
                    DefaultResponse = defaultResponse,
                    Parameters = operation.Parameters.Select(p =>
                    {
                        if (p.ActualSchema.Type == JsonObjectType.File)
                        {
                            p.ActualSchema.Type = JsonObjectType.String;     // TODO: Implement File type handling
                        }
                        return new ParameterModel
                        {
                            Schema = p.ActualSchema,
                            Name = p.Name,
                            VariableNameLower = ConversionUtilities.ConvertToLowerCamelCase(p.Name.Replace("-", "_").Replace(".", "_")),
                            Kind = p.Kind,
                            IsRequired = p.IsRequired,
                            Type = ResolveParameterType(p, resolver),
                            IsLast = operation.Parameters.LastOrDefault() == p,
                            Description = ConversionUtilities.TrimWhiteSpaces(p.Description)
                        };
                    }).ToList(),
                });
            }).ToList();

            return(operations);
        }
예제 #6
0
 internal PropertyModel(JsonProperty property, CSharpTypeResolver resolver, CSharpGeneratorSettings settings) : base(property)
 {
     Name           = property.Name;
     HasDescription = !string.IsNullOrEmpty(property.Description);
     Description    = ConversionUtilities.RemoveLineBreaks(property.Description);
     PropertyName   = ConversionUtilities.ConvertToUpperCamelCase(property.Name);
     FieldName      = ConversionUtilities.ConvertToLowerCamelCase(property.Name);
     Required       = property.IsRequired && settings.RequiredPropertiesMustBeDefined ? "Required.Always" : "Required.Default";
     IsStringEnum   = property.ActualPropertySchema.IsEnumeration && property.ActualPropertySchema.Type == JsonObjectType.String;
     Type           = resolver.Resolve(property.ActualPropertySchema, property.IsNullable, property.Name);
 }
예제 #7
0
 /// <summary>Generates the property name.</summary>
 /// <param name="property">The property.</param>
 /// <returns>The new name.</returns>
 public virtual string Generate(JsonProperty property)
 {
     return(ConversionUtilities.ConvertToLowerCamelCase(property.Name
                                                        .Replace("\"", string.Empty)
                                                        .Replace("@", string.Empty)
                                                        .Replace(".", "-")
                                                        .Replace("=", "-")
                                                        .Replace("+", "plus"), true)
            .Replace("*", "Star")
            .Replace(":", "_")
            .Replace("-", "_"));
 }
예제 #8
0
        /// <summary>Gets the name of the parameter variable.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <param name="allParameters">All parameters.</param>
        /// <returns>The parameter variable name.</returns>
        protected virtual string GetParameterVariableName(SwaggerParameter parameter, IEnumerable <SwaggerParameter> allParameters)
        {
            var variableName = ConversionUtilities.ConvertToLowerCamelCase(parameter.Name
                                                                           .Replace("-", "_")
                                                                           .Replace(".", "_")
                                                                           .Replace("$", string.Empty), true);

            if (allParameters.Count(p => p.Name == parameter.Name) > 1)
            {
                return(variableName + parameter.Kind);
            }

            return(variableName);
        }
        /// <summary>Generates the property name.</summary>
        /// <param name="property">The property.</param>
        /// <returns>The new name.</returns>
        public virtual string Generate(JsonSchemaProperty property)
        {
            var name = ConversionUtilities.ConvertToLowerCamelCase(property.Name
                                                                   .Replace("\"", string.Empty)
                                                                   .Replace("@", string.Empty)
                                                                   .Replace(".", "-")
                                                                   .Replace("=", "-")
                                                                   .Replace("+", "plus"), true)
                       .Replace("*", "Star")
                       .Replace(":", "_")
                       .Replace("-", "_");

            if (ReservedPropertyNames.Contains(name))
            {
                return(name + "_");
            }

            return(name);
        }
        /// <summary>Generates the parameter name for the given parameter.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <param name="allParameters">All parameters.</param>
        /// <returns>The parameter name.</returns>
        public string Generate(SwaggerParameter parameter, IEnumerable <SwaggerParameter> allParameters)
        {
            if (string.IsNullOrEmpty(parameter.Name))
            {
                return("unnamed");
            }

            var variableName = ConversionUtilities.ConvertToLowerCamelCase(parameter.Name
                                                                           .Replace("-", "_")
                                                                           .Replace(".", "_")
                                                                           .Replace("$", string.Empty)
                                                                           .Replace("[", string.Empty)
                                                                           .Replace("]", string.Empty), true);

            if (allParameters.Count(p => p.Name == parameter.Name) > 1)
            {
                return(variableName + parameter.Kind);
            }

            return(variableName);
        }
예제 #11
0
        public PropertyModel(JsonProperty property, TypeScriptTypeResolver resolver, TypeScriptGeneratorSettings settings, TypeScriptGenerator generator)
            : base(property)
        {
            var propertyName = ConversionUtilities.ConvertToLowerCamelCase(property.Name).Replace("-", "_");

            Name               = property.Name;
            InterfaceName      = property.Name.Contains("-") ? '\"' + property.Name + '\"' : property.Name;
            PropertyName       = propertyName;
            Type               = resolver.Resolve(property.ActualPropertySchema, property.IsNullable, property.Name);
            DataConversionCode = settings.TypeStyle == TypeScriptTypeStyle.Interface ? string.Empty : generator.GenerateDataConversion(
                settings.TypeStyle == TypeScriptTypeStyle.Class ? "this." + propertyName : propertyName,
                "data[\"" + property.Name + "\"]",
                property.ActualPropertySchema,
                property.IsNullable,
                property.Name);
            Description    = property.Description;
            HasDescription = !string.IsNullOrEmpty(property.Description);
            IsArray        = property.ActualPropertySchema.Type.HasFlag(JsonObjectType.Array);
            ArrayItemType  = resolver.TryResolve(property.ActualPropertySchema.Item, property.Name);
            IsReadOnly     = property.IsReadOnly && settings.GenerateReadOnlyKeywords;
            IsOptional     = !property.IsRequired;
        }
        public string Generate(OpenApiParameter parameter, IEnumerable <OpenApiParameter> allParameters)
        {
            const string customHeaderPrefix = "x_";

            if (string.IsNullOrEmpty(parameter.Name))
            {
                return("unnamed");
            }

            var lowerCamelCase = ConversionUtilities.ConvertToLowerCamelCase(parameter.Name.Replace("-", "_")
                                                                             .Replace(".", "_").Replace("$", string.Empty)
                                                                             .Replace("[", string.Empty)
                                                                             .Replace("]", string.Empty), true);

            if (lowerCamelCase.Length <= customHeaderPrefix.Length || !lowerCamelCase.StartsWith(customHeaderPrefix))
            {
                return(lowerCamelCase);
            }

            lowerCamelCase = lowerCamelCase.Remove(0, customHeaderPrefix.Length);
            lowerCamelCase = char.ToLowerInvariant(lowerCamelCase[0]) + lowerCamelCase.Substring(1);

            return(lowerCamelCase);
        }
예제 #13
0
 public static string Lowercamelcase(Context context, string input, bool firstCharacterMustBeAlpha = true)
 {
     return(ConversionUtilities.ConvertToLowerCamelCase(input, firstCharacterMustBeAlpha));
 }
예제 #14
0
        public async Task GenerateTS()
        {
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToTypeScriptClientGeneratorSettings()
            {
                Template = TypeScriptTemplate.Fetch,
                OperationNameGenerator = new CustomNameGenerator()
            };

            settings.TypeScriptGeneratorSettings.PropertyNameGenerator = new MyPropertyNameGenerator(c => ConversionUtilities.ConvertToLowerCamelCase(c, true));
            settings.TypeScriptGeneratorSettings.NullValue             = NJsonSchema.CodeGeneration.TypeScript.TypeScriptNullValue.Null;


            var generator = new SwaggerToTypeScriptClientGenerator(document, settings);
            var ts        = generator.GenerateFile();

            ts = "namespace " + Namespace + " {\n" + ConversionUtilities.Tab(ts, 1).TrimEnd('\n') + "\n}\n";
            Context.ReturnFile(Encoding.UTF8.GetBytes(ts), "ApiClient.ts", "text/plain");
            //File.WriteAllText(TSPath, generator.GenerateFile());
        }
예제 #15
0
 public static string ToCamelCase(this string @this) => ConversionUtilities.ConvertToLowerCamelCase(@this, true);
 //PT HACK - da wir die funktion Generate(JsonProperty xx) nicht benutzen können wurde hier um eine mit stringparam erweitert
 //Ich habe die obige funktion angepasst damit wir hier nicht duplicate code haben
 /// <summary>Generates the property name from string.</summary>
 /// <param name="propertyName">The property name string.</param>
 /// <returns>The new name.</returns>
 public virtual string Generate(string propertyName)
 {
     return(ConversionUtilities.ConvertToLowerCamelCase(propertyName, true).Replace("-", "_"));
 }
예제 #17
0
        public static void GenerateTS(SwaggerDocument document, ApiClientDefinition definition)
        {
            var settings = new SwaggerToTypeScriptClientGeneratorSettings()
            {
                Template = TypeScriptTemplate.Fetch,
                OperationNameGenerator     = new CustomNameGenerator(),
                GenerateOptionalParameters = true
            };

            settings.TypeScriptGeneratorSettings.PropertyNameGenerator = new MyPropertyNameGenerator(c => ConversionUtilities.ConvertToLowerCamelCase(c, true));
            settings.TypeScriptGeneratorSettings.NullValue             = TypeScriptNullValue.Null;


            var generator = new SwaggerToTypeScriptClientGenerator(document, settings);
            var ts        = generator.GenerateFile();

            ts = "namespace " + definition.Namespace + " {\n    " + ConversionUtilities.Tab(ts, 1).TrimEnd('\n') + "\n}\n";
            File.WriteAllText(definition.TypescriptClient, ts);

            if (definition.CompileTypescript)
            {
                Process.Start("tsc", definition.TypescriptClient).WaitForExit();
            }
        }
예제 #18
0
        public static void GenerateTS(SwaggerDocument document, ApiClientDefinition definition)
        {
            var settings = new SwaggerToTypeScriptClientGeneratorSettings()
            {
                Template = TypeScriptTemplate.Fetch,
                OperationNameGenerator     = new CustomNameGenerator(),
                GenerateOptionalParameters = true,
                UseTransformOptionsMethod  = true,
                ClientBaseClass            = "ClientBase"
            };

            settings.TypeScriptGeneratorSettings.PropertyNameGenerator = new MyPropertyNameGenerator(c => ConversionUtilities.ConvertToLowerCamelCase(c, true));
            settings.TypeScriptGeneratorSettings.NullValue             = TypeScriptNullValue.Null;
            settings.TypeScriptGeneratorSettings.TemplateFactory       = new DotvvmClientTemplateFactory(settings.CodeGeneratorSettings, new[] {
                typeof(TypeScriptGeneratorSettings).Assembly,
                typeof(SwaggerToTypeScriptClientGeneratorSettings).Assembly
            });

            var resolver  = new TypeScriptTypeResolver(settings.TypeScriptGeneratorSettings);
            var generator = new DotvvmSwaggerToTypeScriptClientGenerator(document, settings, resolver);
            var ts        = generator.GenerateFile();
            var baseClass = CreateBaseClass(definition);

            ts = WrapInNamespace(definition, ts, baseClass);
            File.WriteAllText(definition.TypescriptClient, ts);

            if (definition.CompileTypescript)
            {
                Process.Start(new ProcessStartInfo()
                {
                    FileName        = "tsc",
                    Arguments       = definition.TypescriptClient,
                    UseShellExecute = true,
                    CreateNoWindow  = true,
                    WindowStyle     = ProcessWindowStyle.Hidden
                }).WaitForExit();
            }
        }
예제 #19
0
        public static void GenerateTS(SwaggerDocument document, ApiClientDefinition definition)
        {
            var settings = new SwaggerToTypeScriptClientGeneratorSettings()
            {
                Template = TypeScriptTemplate.Fetch,
                OperationNameGenerator     = new CustomNameGenerator(),
                GenerateOptionalParameters = true,
                UseTransformOptionsMethod  = true,
                ClientBaseClass            = "ClientBase"
            };

            settings.TypeScriptGeneratorSettings.PropertyNameGenerator = new MyPropertyNameGenerator(c => ConversionUtilities.ConvertToLowerCamelCase(c, true));
            settings.TypeScriptGeneratorSettings.NullValue             = TypeScriptNullValue.Null;


            var generator = new SwaggerToTypeScriptClientGenerator(document, settings);
            var ts        = generator.GenerateFile();
            var baseClass = CreateBaseClass(definition);

            ts = WrapInNamespace(definition, ts, baseClass);
            File.WriteAllText(definition.TypescriptClient, ts);

            if (definition.CompileTypescript)
            {
                Process.Start("tsc", definition.TypescriptClient).WaitForExit();
            }
        }