Example #1
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));
            }
        }
Example #2
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));
                    }
                }
            }
        }