Exemplo n.º 1
0
        private static ServiceTypeInfo TryCreateMethodResponseBodyType(HttpMethodInfo httpMethodInfo, HttpResponseInfo httpResponseInfo)
        {
            if (httpResponseInfo.NormalFields == null || httpResponseInfo.NormalFields.Count == 0)
            {
                return(null);
            }

            return(ServiceTypeInfo.CreateDto(new ServiceDtoInfo(
                                                 name: $"{CodeGenUtility.ToPascalCase(httpMethodInfo.ServiceMethod.Name)}Response",
                                                 fields: httpResponseInfo.NormalFields.Select(x => x.ServiceField))));
        }
        private void Convert(SwaggerParserContext context)
        {
            if (m_swaggerService.Swagger == null)
            {
                m_errors.Add(context.CreateError("swagger field is missing."));
            }
            else if (m_swaggerService.Swagger != SwaggerUtility.SwaggerVersion)
            {
                m_errors.Add(context.CreateError($"swagger should be '{SwaggerUtility.SwaggerVersion}'.", "swagger"));
            }

            if (m_swaggerService.Info == null)
            {
                m_errors.Add(context.CreateError("info is missing."));
            }

            var name = m_serviceName;

            if (name != null && !ServiceDefinitionUtility.IsValidName(name))
            {
                m_errors.Add(context.CreateError("ServiceName generator option is not a valid service name."));
            }
            if (name == null)
            {
                name = m_swaggerService.Info?.Identifier;
            }
            if (name != null && !ServiceDefinitionUtility.IsValidName(name))
            {
                m_errors.Add(context.CreateError("info/x-identifier is not a valid service name.", "info/x-identifier"));
            }
            if (name == null && m_swaggerService.Info?.Title is string title)
            {
                name = CodeGenUtility.ToPascalCase(title);
            }
            if (name == null)
            {
                m_errors.Add(context.CreateError("info/title is missing.", "info"));
            }
            if (name != null && !ServiceDefinitionUtility.IsValidName(name))
            {
                m_errors.Add(context.CreateError("info/title is not a valid service name.", "info/title"));
            }

            var attributes = new List <ServiceAttributeInfo>();

            var version = m_swaggerService.Info?.Version;

            if (!string.IsNullOrWhiteSpace(version))
            {
                attributes.Add(new ServiceAttributeInfo("info",
                                                        new[] { new ServiceAttributeParameterInfo("version", version !, context.CreatePart("info/version") !) },
Exemplo n.º 3
0
 public void PascalCase(string before, string after)
 {
     CodeGenUtility.ToPascalCase(before).Should().Be(after);
 }
Exemplo n.º 4
0
        private ServiceInfo ConvertSwaggerService(SwaggerService swaggerService, SwaggerParserContext context)
        {
            if (swaggerService.Swagger == null)
            {
                throw context.CreateException("swagger field is missing.");
            }
            if (swaggerService.Swagger != SwaggerUtility.SwaggerVersion)
            {
                throw context.CreateException($"swagger should be '{SwaggerUtility.SwaggerVersion}'.", "swagger");
            }

            if (swaggerService.Info == null)
            {
                throw context.CreateException("info is missing.");
            }

            string name = ServiceName;

            if (name != null && !ServiceDefinitionUtility.IsValidName(name))
            {
                throw context.CreateException("ServiceName generator option is not a valid service name.");
            }
            if (name == null)
            {
                name = swaggerService.Info?.Identifier;
            }
            if (name != null && !ServiceDefinitionUtility.IsValidName(name))
            {
                throw context.CreateException("info/x-identifier is not a valid service name.", "info/x-identifier");
            }
            if (name == null)
            {
                name = CodeGenUtility.ToPascalCase(swaggerService.Info?.Title);
            }
            if (name == null)
            {
                throw context.CreateException("info/title is missing.", "info");
            }
            if (name != null && !ServiceDefinitionUtility.IsValidName(name))
            {
                throw context.CreateException("info/title is not a valid service name.", "info/title");
            }

            var attributes = new List <ServiceAttributeInfo>();

            string version = swaggerService.Info?.Version;

            if (!string.IsNullOrWhiteSpace(version))
            {
                attributes.Add(new ServiceAttributeInfo("info",
                                                        new[] { new ServiceAttributeParameterInfo("version", version, context.CreatePosition("info/version")) },
                                                        context.CreatePosition("info")));
            }

            string scheme   = GetBestScheme(swaggerService.Schemes);
            string host     = swaggerService.Host;
            string basePath = swaggerService.BasePath ?? "";

            if (!string.IsNullOrWhiteSpace(host) && !string.IsNullOrWhiteSpace(scheme))
            {
                string url = new UriBuilder(scheme, host)
                {
                    Path = basePath
                }.Uri.AbsoluteUri;
                attributes.Add(new ServiceAttributeInfo("http",
                                                        new[] { new ServiceAttributeParameterInfo("url", url, context.CreatePosition()) },
                                                        context.CreatePosition()));
            }

            var position = context.CreatePosition();

            var members = new List <IServiceMemberInfo>();

            foreach (var swaggerPath in swaggerService.Paths.EmptyIfNull())
            {
                var swaggerOperations = swaggerPath.Value;
                var operationsContext = context.CreateContext("paths/swaggerPath");
                swaggerService.ResolveOperations(ref swaggerOperations, ref operationsContext);
                AddServiceMethod(members, "GET", swaggerPath.Key, swaggerOperations.Get, swaggerOperations.Parameters, swaggerService, operationsContext.CreateContext("get"));
                AddServiceMethod(members, "POST", swaggerPath.Key, swaggerOperations.Post, swaggerOperations.Parameters, swaggerService, operationsContext.CreateContext("post"));
                AddServiceMethod(members, "PUT", swaggerPath.Key, swaggerOperations.Put, swaggerOperations.Parameters, swaggerService, operationsContext.CreateContext("put"));
                AddServiceMethod(members, "DELETE", swaggerPath.Key, swaggerOperations.Delete, swaggerOperations.Parameters, swaggerService, operationsContext.CreateContext("delete"));
                AddServiceMethod(members, "OPTIONS", swaggerPath.Key, swaggerOperations.Options, swaggerOperations.Parameters, swaggerService, operationsContext.CreateContext("options"));
                AddServiceMethod(members, "HEAD", swaggerPath.Key, swaggerOperations.Head, swaggerOperations.Parameters, swaggerService, operationsContext.CreateContext("head"));
                AddServiceMethod(members, "PATCH", swaggerPath.Key, swaggerOperations.Patch, swaggerOperations.Parameters, swaggerService, operationsContext.CreateContext("patch"));
            }

            foreach (var swaggerDefinition in swaggerService.Definitions.EmptyIfNull())
            {
                if ((swaggerDefinition.Value.Type ?? SwaggerSchemaType.Object) == SwaggerSchemaType.Object &&
                    !members.OfType <ServiceMethodInfo>().Any(x => swaggerDefinition.Key.Equals(x.Name + "Request", StringComparison.OrdinalIgnoreCase)) &&
                    !members.OfType <ServiceMethodInfo>().Any(x => swaggerDefinition.Key.Equals(x.Name + "Response", StringComparison.OrdinalIgnoreCase)) &&
                    !swaggerService.IsFacilityError(swaggerDefinition) &&
                    swaggerService.TryGetFacilityResultOfType(swaggerDefinition, position) == null)
                {
                    AddServiceDto(members, swaggerDefinition.Key, swaggerDefinition.Value, swaggerService, context.CreatePosition("definitions/" + swaggerDefinition.Key));
                }
            }

            return(new ServiceInfo(name, members: members, attributes: attributes,
                                   summary: PrepareSummary(swaggerService.Info?.Title),
                                   remarks: SplitRemarks(swaggerService.Info?.Description),
                                   position: context.CreatePosition()));
        }