コード例 #1
0
ファイル: ReturnTypeModel.cs プロジェクト: torutek/SigSpec
        public ReturnTypeModel(JsonSchema4 parameter, TypeResolverBase resolver)
        {
            _parameter = parameter;
            _resolver  = resolver;

            Type = _resolver.Resolve(_parameter.ActualTypeSchema, _parameter.IsNullable(SchemaType.JsonSchema), null);
        }
コード例 #2
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(r => new ResponseModel(this)
                {
                    StatusCode = r.Key,
                    Schema     = r.Value.Schema?.ActualSchema
                }).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),
                    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 = ConvertToLowerCamelCase(p.Name.Replace("-", "_")),
                            Kind = p.Kind,
                            IsRequired = p.IsRequired,
                            Type = resolver.Resolve(p.ActualSchema, p.Type.HasFlag(JsonObjectType.Null), p.Name),
                            IsLast = operation.Parameters.LastOrDefault() == p,
                            Description = RemoveLineBreaks(p.Description)
                        };
                    }).ToList(),
                });
            }).ToList();

            return(operations);
        }
コード例 #3
0
        private string ResolveParameterType <TGenerator>(SwaggerParameter parameter, TypeResolverBase <TGenerator> resolver)
            where TGenerator : TypeGeneratorBase
        {
            var schema = parameter.ActualParameterSchema;

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

            return(resolver.Resolve(schema, !parameter.IsRequired && parameter.IsNullable, parameter.Name));
        }
コード例 #4
0
        private string ResolveParameterType <TGenerator>(SwaggerParameter parameter, TypeResolverBase <TGenerator> resolver)
            where TGenerator : TypeGeneratorBase
        {
            var schema = parameter.ActualSchema;

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

            return(resolver.Resolve(schema, parameter.IsRequired == false || parameter.IsNullable(BaseSettings.CodeGeneratorSettings.PropertyNullHandling), parameter.Name));
        }
コード例 #5
0
ファイル: ClientGeneratorBase.cs プロジェクト: csnyded3/NSwag
        private string ResolveParameterType <TGenerator>(SwaggerParameter parameter, TypeResolverBase <TGenerator> resolver)
            where TGenerator : TypeGeneratorBase
        {
            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);

            return(resolver.Resolve(schema, parameter.IsRequired == false || parameter.IsNullable(BaseSettings.CodeGeneratorSettings.NullHandling), typeNameHint));
        }
コード例 #6
0
ファイル: OperationModelBase.cs プロジェクト: zhutoutou/NSwag
        /// <summary>Resolves the type of the parameter.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The parameter type name.</returns>
        protected virtual string ResolveParameterType(OpenApiParameter parameter)
        {
            var schema = parameter.ActualSchema;

            if (parameter.IsXmlBodyParameter)
            {
                return("string");
            }

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

            var typeNameHint = !schema.HasTypeNameTitle ? ConversionUtilities.ConvertToUpperCamelCase(parameter.Name, true) : null;
            var isNullable   = parameter.IsRequired == false || parameter.IsNullable(_settings.CodeGeneratorSettings.SchemaType);

            return(_resolver.Resolve(schema, isNullable, typeNameHint));
        }
コード例 #7
0
ファイル: ClientGeneratorBase.cs プロジェクト: geekmi/NSwag
        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 pathSegments = tuple.Path.Split('/').Where(p => !p.Contains("{")).Reverse().ToArray();

                var mvcControllerName = pathSegments.Length >= 2 ? pathSegments[1] : "Unknown";
                var mvcActionName     = pathSegments.Length >= 1 ? pathSegments[0] : "Unknown";

                var operation = tuple.Operation;
                var responses = operation.Responses.Select(r => new ResponseModel
                {
                    StatusCode = r.Key,
                    IsSuccess  = HttpUtilities.IsSuccessStatusCode(r.Key),
                    Type       = GetType(r.Value.Schema, "Response"),
                    TypeIsDate = GetType(r.Value.Schema, "Response") == "Date"
                }).ToList();

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

                return(new OperationModel
                {
                    Id = operation.OperationId,

                    Path = tuple.Path,

                    HttpMethodUpper = ConvertToUpperStartIdentifier(tuple.HttpMethod.ToString()),
                    HttpMethodLower = ConvertToLowerStartIdentifier(tuple.HttpMethod.ToString()),

                    IsGetOrDelete = tuple.HttpMethod == SwaggerOperationMethod.Get || tuple.HttpMethod == SwaggerOperationMethod.Delete,

                    Summary = RemoveLineBreaks(operation.Summary),

                    MvcActionName = mvcActionName,
                    MvcControllerName = mvcControllerName,

                    OperationNameLower =
                        ConvertToLowerStartIdentifier(BaseSettings.OperationGenerationMode == OperationGenerationMode.MultipleClientsFromPathSegments
                                ? mvcActionName
                                : operation.OperationId),
                    OperationNameUpper =
                        ConvertToUpperStartIdentifier(BaseSettings.OperationGenerationMode == OperationGenerationMode.MultipleClientsFromPathSegments
                                ? mvcActionName
                                : operation.OperationId),

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

                    ExceptionType = GetExceptionType(operation),

                    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 = ConvertToLowerStartIdentifier(p.Name.Replace("-", "_")),
                            Kind = p.Kind,
                            IsOptional = !p.IsRequired,
                            Type = resolver.Resolve(p.ActualSchema, p.IsRequired, p.Name),
                            IsLast = operation.Parameters.LastOrDefault() == p,
                            Description = RemoveLineBreaks(p.Description)
                        };
                    }).ToList(),
                });
            }).ToList();

            return(operations);
        }
コード例 #8
0
ファイル: ParameterModel.cs プロジェクト: austinsc/SigSpec
 public ParameterModel(string name, SigSpecParameter parameter, TypeResolverBase resolver)
 {
     this.Name = name;
     this.Type = resolver.Resolve(parameter.ActualTypeSchema, parameter.IsNullable(SchemaType.JsonSchema), this.Name);
 }
コード例 #9
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 pathSegments = tuple.Path.Split('/').Where(p => !p.Contains("{")).Reverse().ToArray();

                var mvcControllerName = pathSegments.Length >= 2 ? pathSegments[1] : "Unknown";
                var mvcActionName     = pathSegments.Length >= 1 ? pathSegments[0] : "Unknown";

                var operation = tuple.Operation;
                var responses = operation.Responses.Select(r => new ResponseModel
                {
                    StatusCode = r.Key,
                    IsSuccess  = r.Key == "200",
                    Type       = GetType(r.Value.Schema, "Response"),
                    TypeIsDate = GetType(r.Value.Schema, "Response") == "Date"
                }).ToList();

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

                return(new OperationModel
                {
                    Id = operation.OperationId,

                    Path = tuple.Path,

                    HttpMethodUpper = ConvertToUpperStartIdentifier(tuple.HttpMethod.ToString()),
                    HttpMethodLower = ConvertToLowerStartIdentifier(tuple.HttpMethod.ToString()),

                    IsGetOrDelete = tuple.HttpMethod == SwaggerOperationMethod.Get || tuple.HttpMethod == SwaggerOperationMethod.Delete,

                    Summary = RemoveLineBreaks(operation.Summary),

                    MvcActionName = mvcActionName,
                    MvcControllerName = mvcControllerName,

                    OperationNameLower =
                        ConvertToLowerStartIdentifier(OperationGenerationMode == OperationGenerationMode.MultipleClientsFromPathSegments
                                ? mvcActionName
                                : operation.OperationId),
                    OperationNameUpper =
                        ConvertToUpperStartIdentifier(OperationGenerationMode == OperationGenerationMode.MultipleClientsFromPathSegments
                                ? mvcActionName
                                : operation.OperationId),

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

                    ExceptionType = GetExceptionType(operation),

                    Responses = responses,
                    DefaultResponse = defaultResponse,

                    Parameters = operation.Parameters.Select(p => new ParameterModel
                    {
                        Name = p.Name,
                        Type = resolver.Resolve(p.ActualSchema, p.IsRequired, p.Name),
                        IsLast = operation.Parameters.LastOrDefault() == p,
                        Description = RemoveLineBreaks(p.Description)
                    }).ToList(),

                    ContentParameter =
                        operation.Parameters.Where(p => p.Kind == SwaggerParameterKind.Body)
                        .Select(p => new ParameterModel {
                        Name = p.Name
                    })
                        .SingleOrDefault(),

                    PlaceholderParameters =
                        operation.Parameters.Where(p => p.Kind == SwaggerParameterKind.Path).Select(p => new ParameterModel
                    {
                        Name = p.Name,
                        IsDate = p.Format == JsonFormatStrings.DateTime,
                        Description = RemoveLineBreaks(p.Description)
                    }),

                    QueryParameters =
                        operation.Parameters.Where(p => p.Kind == SwaggerParameterKind.Query).Select(p => new ParameterModel
                    {
                        Name = p.Name,
                        IsDate = p.Format == JsonFormatStrings.DateTime,
                        Description = RemoveLineBreaks(p.Description)
                    }).ToList(),
                });
            }).ToList();

            return(operations);
        }