Exemple #1
0
 internal virtual string GetParameterVariableName(SwaggerParameter parameter)
 {
     return ConversionUtilities.ConvertToLowerCamelCase(parameter.Name
         .Replace("-", "_")
         .Replace(".", "_")
         .Replace("$", string.Empty), true);
 }
Exemple #2
0
        /// <summary>Initializes a new instance of the <see cref="ParameterModel" /> class.</summary>
        /// <param name="typeName">The type name.</param>
        /// <param name="operation">The operation.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="parameterName">Name of the parameter.</param>
        /// <param name="variableName">Name of the variable.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="clientGeneratorBase">The client generator base.</param>
        public ParameterModel(string typeName, SwaggerOperation operation, SwaggerParameter parameter, 
            string parameterName, string variableName, CodeGeneratorSettingsBase settings, ClientGeneratorBase clientGeneratorBase)
        {
            Type = typeName;
            Name = parameterName;
            VariableName = variableName;

            _operation = operation;
            _parameter = parameter;
            _settings = settings;
            _clientGeneratorBase = clientGeneratorBase;
        }
        /// <summary>Resolves the type of the parameter.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The parameter type name.</returns>
        protected override string ResolveParameterType(SwaggerParameter parameter)
        {
            var schema = parameter.ActualSchema;
            if (schema.Type == JsonObjectType.File)
            {
                if (parameter.CollectionFormat == SwaggerParameterCollectionFormat.Multi && !schema.Type.HasFlag(JsonObjectType.Array))
                    return "FileParameter[]";

                return "FileParameter";
            }

            return base.ResolveParameterType(parameter);
        }
        /// <summary>Resolves the type of the parameter.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The parameter type name.</returns>
        protected override string ResolveParameterType(SwaggerParameter parameter)
        {
            var schema = parameter.ActualSchema;
            if (schema.Type == JsonObjectType.File)
            {
                if (parameter.CollectionFormat == SwaggerParameterCollectionFormat.Multi && !schema.Type.HasFlag(JsonObjectType.Array))
                    return "System.Collections.Generic.IEnumerable<FileParameter>";

                return "FileParameter";
            }

            return base.ResolveParameterType(parameter)
                .Replace(Settings.CSharpGeneratorSettings.ArrayType + "<", "System.Collections.Generic.IEnumerable<")
                .Replace(Settings.CSharpGeneratorSettings.DictionaryType + "<", "System.Collections.Generic.IDictionary<");
        }
Exemple #5
0
        /// <summary>Creates a path parameter for a given type.</summary>
        /// <param name="parameterName">Name of the parameter.</param>
        /// <param name="parameterType">Type of the parameter.</param>
        /// <returns>The parameter.</returns>
        public SwaggerParameter CreatePathParameter(string parameterName, string parameterType)
        {
            var parameter = new SwaggerParameter();
            parameter.Name = parameterName;
            parameter.Kind = SwaggerParameterKind.Path;
            parameter.IsRequired = true;
            parameter.IsNullableRaw = false;

            if (parameterType == "guid")
            {
                parameter.Type = JsonObjectType.String;
                parameter.Format = JsonFormatStrings.Guid;
            }
            else if (parameterType == "int" || parameterType == "integer" || parameterType == "short" || parameterType == "long")
                parameter.Type = JsonObjectType.Integer;
            else if (parameterType == "number" || parameterType == "decimal" || parameterType == "double")
                parameter.Type = JsonObjectType.Number;
            else
                parameter.Type = JsonObjectType.String;

            return parameter;
        }
Exemple #6
0
        /// <summary>Resolves the type of the parameter.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The parameter type name.</returns>
        protected virtual string ResolveParameterType(SwaggerParameter parameter)
        {
            var schema = parameter.ActualSchema;

            if (parameter.CollectionFormat == SwaggerParameterCollectionFormat.Multi && !schema.Type.HasFlag(JsonObjectType.Array))
                schema = new JsonSchema4 { Type = JsonObjectType.Array, Item = schema };

            var typeNameHint = ConversionUtilities.ConvertToUpperCamelCase(parameter.Name, true);
            return Resolver.Resolve(schema, parameter.IsRequired == false || parameter.IsNullable(BaseSettings.CodeGeneratorSettings.NullHandling), typeNameHint);
        }
Exemple #7
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="parameterType">Type of the parameter.</param>
        /// <param name="parentAttributes">The parent attributes.</param>
        /// <returns></returns>
        public SwaggerParameter CreatePrimitiveParameter(string name, string description, Type parameterType, IList<Attribute> parentAttributes)
        {
            var typeDescription = JsonObjectTypeDescription.FromType(parameterType, parentAttributes, _settings.DefaultEnumHandling);

            SwaggerParameter operationParameter;
            if (typeDescription.IsEnum)
            {
                // TODO(incompatibility): We use "schema" even it is not allowed in non-body parameters
                parameterType = parameterType.Name == "Nullable`1" ? parameterType.GetGenericTypeArguments().Single() : parameterType;
                operationParameter = new SwaggerParameter
                {
                    Type = typeDescription.Type, // Used as fallback for generators which do not check the "schema" property
                    Schema = new JsonSchema4
                    {
                        SchemaReference = _schemaGenerator.Generate<JsonSchema4>(parameterType, parentAttributes, _schemaResolver)
                    }
                };
            }
            else
            {
                parameterType = typeDescription.Type.HasFlag(JsonObjectType.Object) ? typeof(string) : parameterType; // object types must be treated as string

                operationParameter = _schemaGenerator.Generate<SwaggerParameter>(parameterType, parentAttributes, _schemaResolver);
                _schemaGenerator.ApplyPropertyAnnotations(operationParameter, parameterType, parentAttributes, typeDescription);
            }

            operationParameter.Name = name;
            operationParameter.IsRequired = parentAttributes?.Any(a => a.GetType().Name == "RequiredAttribute") ?? false;
            operationParameter.IsNullableRaw = typeDescription.IsNullable;

            if (description != string.Empty)
                operationParameter.Description = description;

            return operationParameter;
        }
Exemple #8
0
        /// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
        /// <param name="name">The name.</param>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        public SwaggerParameter CreateBodyParameter(string name, ParameterInfo parameter)
        {
            var isRequired = IsParameterRequired(parameter);

            var typeDescription = JsonObjectTypeDescription.FromType(parameter.ParameterType, parameter.GetCustomAttributes(), _settings.DefaultEnumHandling);
            var operationParameter = new SwaggerParameter
            {
                Name = name,
                Kind = SwaggerParameterKind.Body,
                IsRequired = isRequired,
                IsNullableRaw = typeDescription.IsNullable,
                Schema = GenerateAndAppendSchemaFromType(parameter.ParameterType, !isRequired, parameter.GetCustomAttributes()),
            };

            var description = parameter.GetXmlDocumentation();
            if (description != string.Empty)
                operationParameter.Description = description;

            return operationParameter;
        }
        private void InitializeFileParameter(SwaggerParameter operationParameter, bool isFileArray)
        {
            operationParameter.Type = JsonObjectType.File;
            operationParameter.Kind = SwaggerParameterKind.FormData;

            if (isFileArray)
                operationParameter.CollectionFormat = SwaggerParameterCollectionFormat.Multi;
        }
 internal override string GetParameterVariableName(SwaggerParameter parameter)
 {
     var name = base.GetParameterVariableName(parameter);
     return ReservedKeywords.Contains(name) ? "@" + name : name;
 }