Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Paths in Swagger JSON: Service Name Prefix Missing #312

Open
rabdulatif opened this issue Oct 6, 2024 · 2 comments
Open

Problem with Paths in Swagger JSON: Service Name Prefix Missing #312

rabdulatif opened this issue Oct 6, 2024 · 2 comments
Assignees

Comments

@rabdulatif
Copy link
Contributor

There is a problem with the paths section in the generated Swagger JSON. The paths currently do not include the appropriate service name prefix, causing potential issues with routing and API documentation, especially in microservice-based environments like Ocelot.

Each path in the Swagger JSON should be prefixed with the serviceName associated with the microservice. This ensures that paths are properly scoped and differentiated when working with multiple services

Paths in the Swagger JSON do not contain the necessary serviceName prefix, which leads to incorrect or ambiguous routes in the API documentation

To resolve this issue, I added logic that prefixes the serviceName to each path in the Swagger JSON. This is implemented via the AddServiceNamePrefixToPaths method, which is called within the SwaggerForOcelotMiddleware.

  • The service name is dynamically added at the beginning of each path.
  • This logic ensures that the paths are consistent and accurate for each microservice in the Ocelot gateway.

Steps to Reproduce

  • Generate the Swagger JSON without any path prefixing.
  • Observe that the paths do not include the serviceName.
  • API documentation routes become unclear or incorrect when multiple services are involved.

Suggested Fix
Implement the AddServiceNamePrefixToPaths method to automatically add the serviceName prefix to all paths in the Swagger JSON, ensuring correct routing and clarity in API documentation

public string AddServiceNamePrefixToPaths(string swaggerJson, SwaggerEndPointOptions endPoint, string version)
    {
        var config = string.IsNullOrEmpty(version)
            ? endPoint.Config.FirstOrDefault()
            : endPoint.Config.FirstOrDefault(x => x.Version == version);

        var serviceName = config?.Service?.Name;
        if (string.IsNullOrEmpty(serviceName))
            return swaggerJson;

        if (!swaggerJson.TryParse(out var swaggerObj))
            return swaggerJson;

        if (!swaggerObj.TryGetValue(OpenApiProperties.Paths, out var swaggerPaths))
            return swaggerJson;

        if (swaggerPaths is not JObject pathsObj)
            return swaggerJson;

        var properties = pathsObj.Properties().ToList();
        properties.ForEach(f => SetToPathServiceName(f, pathsObj, serviceName));

        return swaggerObj.ToString();
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="jProperty"></param>
    /// <param name="pathsObj"></param>
    /// <param name="serviceName"></param>
    private void SetToPathServiceName(JProperty jProperty, JObject pathsObj, string serviceName)
    {
        jProperty.Remove();

        var path = $"/{serviceName}{jProperty.Name}";
        pathsObj.Add(path, jProperty.Value);
    }
@rabdulatif
Copy link
Contributor Author

i have some code corrections. After this i will reopen PR

@rabdulatif
Copy link
Contributor Author

PR renewed
please check, if possible can make Nuget Package for new Lib also?
Thanks

@Burgyn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants