Beispiel #1
0
 public string Apply(string value)
 {
     if (value[0] >= 'A' && value[0] <= 'Z')
     {
         value = CodeGenUtility.ToCamelCase(value);
     }
     return(value);
 }
Beispiel #2
0
        private static string GetBodyFieldNameForStatusCode(string statusCode)
        {
            int statusCodeNumber;

            if (int.TryParse(statusCode, out statusCodeNumber))
            {
                string name = ((HttpStatusCode)statusCodeNumber).ToString();
                if (name != statusCode)
                {
                    return(CodeGenUtility.ToCamelCase(name));
                }
            }

            return(CodeGenUtility.ToCamelCase($"status {statusCode}"));
        }
Beispiel #3
0
        private void AddResponseFields(IList <ServiceFieldInfo> responseFields, string statusCode, SwaggerResponse swaggerResponse, string serviceMethodName, IList <ServiceAttributeParameterInfo> httpAttributeValues, bool isOnlyResponse, SwaggerService swaggerService, NamedTextPosition position)
        {
            var bodySchema = default(KeyValuePair <string, SwaggerSchema>);

            if (swaggerResponse.Schema != null)
            {
                bodySchema = swaggerService.ResolveDefinition(swaggerResponse.Schema, position);
            }

            if (bodySchema.Value != null && (bodySchema.Value.Type ?? SwaggerSchemaType.Object) == SwaggerSchemaType.Object &&
                (bodySchema.Key == null || bodySchema.Key.Equals(serviceMethodName + "Response", StringComparison.OrdinalIgnoreCase)))
            {
                httpAttributeValues.Add(new ServiceAttributeParameterInfo("code", statusCode, position));
                AddFieldsFromSchema(responseFields, swaggerService, position, bodySchema);
            }
            else if (swaggerResponse.Identifier == null && isOnlyResponse && swaggerResponse.Schema == null)
            {
                httpAttributeValues.Add(new ServiceAttributeParameterInfo("code", statusCode, position));
            }
            else
            {
                responseFields.Add(new ServiceFieldInfo(
                                       swaggerResponse.Identifier ?? CodeGenUtility.ToCamelCase(bodySchema.Key) ?? GetBodyFieldNameForStatusCode(statusCode),
                                       typeName: bodySchema.Key ?? (bodySchema.Value != null ? SwaggerUtility.FilterBodyTypeName(swaggerService.TryGetFacilityTypeName(bodySchema.Value, position)) : null) ?? "boolean",
                                       attributes: new[]
                {
                    new ServiceAttributeInfo("http",
                                             new[]
                    {
                        new ServiceAttributeParameterInfo("from", "body", position),
                        new ServiceAttributeParameterInfo("code", statusCode, position),
                    })
                },
                                       summary: PrepareSummary(swaggerResponse.Description),
                                       position: position));
            }
        }
 public void CamelCase(string before, string after)
 {
     CodeGenUtility.ToCamelCase(before).Should().Be(after);
 }
Beispiel #5
0
        private void AddRequestFields(IList <ServiceFieldInfo> requestFields, SwaggerParameter swaggerParameter, string serviceMethodName, string httpMethod, SwaggerService swaggerService, NamedTextPosition position)
        {
            string kind = swaggerParameter.In;

            if (kind == SwaggerParameterKind.Path || kind == SwaggerParameterKind.Query || kind == SwaggerParameterKind.Header)
            {
                string typeName = swaggerService.TryGetFacilityTypeName(swaggerParameter, position);
                if (typeName != null)
                {
                    if (typeName.EndsWith("[]", StringComparison.Ordinal))
                    {
                        typeName = "string";
                    }

                    var attributes = new List <ServiceAttributeInfo>();

                    if (swaggerParameter.Obsolete.GetValueOrDefault())
                    {
                        attributes.Add(new ServiceAttributeInfo("obsolete"));
                    }

                    string fieldName = swaggerParameter.Identifier ?? swaggerParameter.Name;
                    if (!ServiceDefinitionUtility.IsValidName(fieldName))
                    {
                        fieldName = CodeGenUtility.ToCamelCase(fieldName);
                    }

                    if (kind == SwaggerParameterKind.Query)
                    {
                        var parameters = new List <ServiceAttributeParameterInfo>();
                        if (httpMethod != "GET")
                        {
                            parameters.Add(new ServiceAttributeParameterInfo("from", "query"));
                        }
                        if (fieldName != swaggerParameter.Name)
                        {
                            parameters.Add(new ServiceAttributeParameterInfo("name", swaggerParameter.Name));
                        }
                        if (parameters.Count != 0)
                        {
                            attributes.Add(new ServiceAttributeInfo("http", parameters));
                        }
                    }
                    else if (kind == SwaggerParameterKind.Header)
                    {
                        attributes.Add(new ServiceAttributeInfo("http",
                                                                new[]
                        {
                            new ServiceAttributeParameterInfo("from", "header"),
                            new ServiceAttributeParameterInfo("name", swaggerParameter.Name),
                        }));
                    }

                    requestFields.Add(new ServiceFieldInfo(
                                          fieldName,
                                          typeName: typeName,
                                          attributes: attributes,
                                          summary: PrepareSummary(swaggerParameter.Description),
                                          position: position));
                }
            }
            else if (kind == SwaggerParameterKind.Body)
            {
                var bodySchema = swaggerService.ResolveDefinition(swaggerParameter.Schema, position);
                if ((bodySchema.Value.Type ?? SwaggerSchemaType.Object) == SwaggerSchemaType.Object &&
                    (bodySchema.Key == null || bodySchema.Key.Equals(serviceMethodName + "Request", StringComparison.OrdinalIgnoreCase)))
                {
                    AddFieldsFromSchema(requestFields, swaggerService, position, bodySchema);
                }
                else
                {
                    string typeName = bodySchema.Key ?? SwaggerUtility.FilterBodyTypeName(swaggerService.TryGetFacilityTypeName(bodySchema.Value, position));
                    if (typeName != null)
                    {
                        requestFields.Add(new ServiceFieldInfo(
                                              bodySchema.Value.Identifier ?? "body",
                                              typeName: typeName,
                                              attributes: new[] { new ServiceAttributeInfo("http", new[] { new ServiceAttributeParameterInfo("from", "body", position) }) },
                                              summary: PrepareSummary(swaggerParameter.Description),
                                              position: position));
                    }
                }
            }
        }
Beispiel #6
0
        private void AddServiceMethod(IList <IServiceMemberInfo> members, string method, string path, SwaggerOperation swaggerOperation, IList <SwaggerParameter> swaggerOperationsParameters, SwaggerService swaggerService, SwaggerParserContext context)
        {
            if (swaggerOperation == null)
            {
                return;
            }

            var position = context.CreatePosition();

            path = s_pathParameter.Replace(path, match =>
            {
                string paramName = match.ToString().Substring(1, match.Length - 2);
                if (!ServiceDefinitionUtility.IsValidName(paramName))
                {
                    paramName = CodeGenUtility.ToCamelCase(paramName);
                }
                return($"{{{paramName}}}");
            });

            string name = CodeGenUtility.ToCamelCase(swaggerOperation.OperationId);

            if (!ServiceDefinitionUtility.IsValidName(name))
            {
                name = CodeGenUtility.ToCamelCase($"{method} {path}");
            }

            var httpAttributeValues = new List <ServiceAttributeParameterInfo>
            {
                new ServiceAttributeParameterInfo("method", method),
                new ServiceAttributeParameterInfo("path", path),
            };

            var requestFields = new List <ServiceFieldInfo>();

            foreach (var swaggerParameter in swaggerOperationsParameters.EmptyIfNull().Concat(swaggerOperation.Parameters.EmptyIfNull()))
            {
                AddRequestFields(requestFields, swaggerService.ResolveParameter(swaggerParameter, position), name, method, swaggerService, position);
            }

            var responseFields       = new List <ServiceFieldInfo>();
            var swaggerResponsePairs = swaggerOperation.Responses.EmptyIfNull()
                                       .Where(x => x.Key[0] == '2' || x.Key[0] == '3' || !string.IsNullOrEmpty(x.Value.Identifier)).ToList();

            foreach (var swaggerResponsePair in swaggerResponsePairs)
            {
                AddResponseFields(responseFields, swaggerResponsePair.Key, swaggerService.ResolveResponse(swaggerResponsePair.Value, position),
                                  name, httpAttributeValues, swaggerOperation.Responses.Count == 1, swaggerService, position);
            }

            var attributes = new List <ServiceAttributeInfo> {
                new ServiceAttributeInfo("http", httpAttributeValues)
            };

            if (swaggerOperation.Deprecated.GetValueOrDefault())
            {
                attributes.Add(new ServiceAttributeInfo("obsolete"));
            }

            members.Add(new ServiceMethodInfo(
                            name: name,
                            requestFields: requestFields,
                            responseFields: responseFields,
                            attributes: attributes,
                            summary: PrepareSummary(swaggerOperation.Summary),
                            remarks: SplitRemarks(swaggerOperation.Description),
                            position: position));
        }