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); }
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())); }