Esempio n. 1
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()));
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        internal static void ResolveOperations(this SwaggerService swaggerService, ref SwaggerOperations swaggerOperations, ref SwaggerParserContext context)
        {
            if (swaggerOperations.Ref != null)
            {
                const string refPrefix = "#/paths/";
                if (!swaggerOperations.Ref.StartsWith(refPrefix, StringComparison.Ordinal))
                {
                    throw new ServiceDefinitionException("Operations $ref must start with '#/paths/'.", context.CreatePosition());
                }

                string name = UnescapeRefPart(swaggerOperations.Ref.Substring(refPrefix.Length));
                if (!swaggerService.Paths.TryGetValue(name, out swaggerOperations))
                {
                    throw new ServiceDefinitionException($"Missing path named '{name}'.", context.CreatePosition());
                }

                context = context.Root.CreateContext("paths/" + name);
            }
        }