Ejemplo n.º 1
0
        internal static string TryGetFacilityTypeName(this SwaggerService swaggerService, ISwaggerSchema swaggerSchema, NamedTextPosition position)
        {
            switch (swaggerSchema.Type ?? SwaggerSchemaType.Object)
            {
            case SwaggerSchemaType.String:
                return(swaggerSchema.Format == SwaggerSchemaTypeFormat.Byte ? "bytes" : "string");

            case SwaggerSchemaType.Number:
                return(swaggerSchema.Format == SwaggerSchemaTypeFormat.Decimal ? "decimal" : "double");

            case SwaggerSchemaType.Integer:
                return(swaggerSchema.Format == SwaggerSchemaTypeFormat.Int64 ? "int64" : "int32");

            case SwaggerSchemaType.Boolean:
                return("boolean");

            case SwaggerSchemaType.Array:
                return(swaggerSchema.Items?.Type == SwaggerSchemaType.Array ? null :
                       $"{swaggerService.TryGetFacilityTypeName(swaggerSchema.Items, position)}[]");

            case SwaggerSchemaType.Object:
                var fullSchema = swaggerSchema as SwaggerSchema;
                if (fullSchema != null)
                {
                    if (fullSchema.Ref != null)
                    {
                        var resolvedSchema = swaggerService.ResolveDefinition(fullSchema, position);

                        if (swaggerService.IsFacilityError(resolvedSchema))
                        {
                            return("error");
                        }

                        string resultOfType = swaggerService.TryGetFacilityResultOfType(resolvedSchema, position);
                        if (resultOfType != null)
                        {
                            return($"result<{resultOfType}>");
                        }

                        return(resolvedSchema.Key);
                    }

                    if (fullSchema.AdditionalProperties != null)
                    {
                        return($"map<{swaggerService.TryGetFacilityTypeName(fullSchema.AdditionalProperties, position)}>");
                    }
                }

                return("object");
            }

            return(null);
        }
Ejemplo n.º 2
0
        private static void AddFieldsFromSchema(IList <ServiceFieldInfo> requestFields, SwaggerService swaggerService, NamedTextPosition position, KeyValuePair <string, SwaggerSchema> bodySchema)
        {
            if ((bodySchema.Value.Type ?? SwaggerSchemaType.Object) != SwaggerSchemaType.Object)
            {
                throw new NotImplementedException();
            }

            foreach (var property in bodySchema.Value.Properties.EmptyIfNull())
            {
                var attributes = new List <ServiceAttributeInfo>();

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

                string typeName = swaggerService.TryGetFacilityTypeName(property.Value, position);
                if (typeName != null)
                {
                    requestFields.Add(new ServiceFieldInfo(
                                          property.Key,
                                          typeName: typeName,
                                          attributes: attributes,
                                          summary: PrepareSummary(property.Value.Description),
                                          position: position));
                }
            }
        }
Ejemplo n.º 3
0
        internal static string TryGetFacilityResultOfType(this SwaggerService swaggerService, KeyValuePair <string, SwaggerSchema> swaggerSchema, NamedTextPosition position)
        {
            const string nameSuffix = "Result";

            if (!swaggerSchema.Key.EndsWith(nameSuffix, StringComparison.Ordinal))
            {
                return(null);
            }

            var properties = swaggerSchema.Value.Properties.EmptyIfNull();

            if (!properties.Any(x => x.Key == "error" && x.Value.Ref == "#/definitions/Error"))
            {
                return(null);
            }

            var valueSchema = properties.Where(x => x.Key == "value").Select(x => x.Value).FirstOrDefault();

            if (valueSchema == null)
            {
                return(null);
            }

            return(swaggerService.TryGetFacilityTypeName(valueSchema, position));
        }
Ejemplo n.º 4
0
        private void AddServiceDto(List <IServiceMemberInfo> members, string name, SwaggerSchema schema, SwaggerService swaggerService, NamedTextPosition position)
        {
            var attributes = new List <ServiceAttributeInfo>();

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

            var fields = new List <ServiceFieldInfo>();

            foreach (var property in schema.Properties.EmptyIfNull())
            {
                var fieldAttributes = new List <ServiceAttributeInfo>();

                if (property.Value.Obsolete.GetValueOrDefault())
                {
                    fieldAttributes.Add(new ServiceAttributeInfo("obsolete"));
                }

                string typeName = swaggerService.TryGetFacilityTypeName(property.Value, position);
                if (typeName != null)
                {
                    fields.Add(new ServiceFieldInfo(
                                   property.Key,
                                   typeName: typeName,
                                   attributes: fieldAttributes,
                                   summary: PrepareSummary(property.Value.Description),
                                   position: position));
                }
            }

            members.Add(new ServiceDtoInfo(
                            name: name,
                            fields: fields,
                            attributes: attributes,
                            summary: PrepareSummary(schema.Description),
                            remarks: SplitRemarks(schema.Remarks),
                            position: position));
        }
Ejemplo n.º 5
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));
            }
        }
Ejemplo n.º 6
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));
                    }
                }
            }
        }