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

Proposal: Change format property in Load and Parse to strongly typed value #1952

Closed
darrelmiller opened this issue Nov 25, 2024 · 1 comment
Labels
priority:p2 Medium. Generally has a work-around and a smaller sub-set of customers is affected. SLA <=30 days type:enhancement Enhancement request targeting an existing experience
Milestone

Comments

@darrelmiller
Copy link
Member

darrelmiller commented Nov 25, 2024

Using the following classes we can create a strongly typed experience for the format property in Parse and Load methods on OpenApiDocument.

// In Microsoft.OpenApi project
public partial class OpenApiFormat {
    private string _format;

    public static OpenApiFormat Json { get; } = new OpenApiFormat("json");

    public OpenApiFormat(string format)
    {
        _format = format.ToLower();
    }

    // Implicit conversion from OpenApiFormat to string
    public static implicit operator string(OpenApiFormat format) => format._format;

    // Implicit conversion from string to OpenApiFormat
    public static implicit operator OpenApiFormat(string format) => new OpenApiFormat(format);

}

// In Microsoft.OpenApi.Yaml project
public partial class OpenApiFormat {
    public static OpenApiFormat Yaml { get; } = new OpenApiFormat("yaml");
}

This will enable calling code like

var readResult = await OpenApiDocument.LoadAsync(streamOpenApiDoc,OpenApiFormat.Json);

var readResult = OpenApiDocument.Load(new StringReader(stringOpenApiDoc), OpenApiFormat.Yaml);

var readResult = OpenApiDocument.Parse(stringOpenApiDoc, OpenApiFormat.Yaml);

This will also give us a good place to put code like this that is just used for determining if content is yaml or json

private static string GetContentType(string url)
{
if (!string.IsNullOrEmpty(url))
{
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
var response = _httpClient.GetAsync(url).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
var mediaType = response.Content.Headers.ContentType.MediaType;
return mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First();
}
return null;
}
/// <summary>
/// Infers the OpenAPI format from the input URL.
/// </summary>
/// <param name="url">The input URL.</param>
/// <returns>The OpenAPI format.</returns>
public static string GetFormat(string url)
{
if (!string.IsNullOrEmpty(url))
{
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
// URL examples ---> https://example.com/path/to/file.json, https://example.com/path/to/file.yaml
var path = new Uri(url);
var urlSuffix = path.Segments[path.Segments.Length - 1].Split('.').LastOrDefault();
return !string.IsNullOrEmpty(urlSuffix) ? urlSuffix : GetContentType(url).Split('/').LastOrDefault();
}
else
{
return Path.GetExtension(url).Split('.').LastOrDefault();
}
}
return null;
}

@darrelmiller darrelmiller added this to the NET:2.0 milestone Nov 26, 2024
@baywet baywet added type:enhancement Enhancement request targeting an existing experience priority:p2 Medium. Generally has a work-around and a smaller sub-set of customers is affected. SLA <=30 days labels Feb 19, 2025
@darrelmiller
Copy link
Member Author

This doesn't work because partial classes can't live in two assemblies. Closing until someone has a better idea. Vincent and Copilot were correct.

@baywet baywet closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority:p2 Medium. Generally has a work-around and a smaller sub-set of customers is affected. SLA <=30 days type:enhancement Enhancement request targeting an existing experience
Projects
None yet
Development

No branches or pull requests

2 participants