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

ObsoleteAttribute is ignored on nested complex types #2760

Closed
mike-loux-planview opened this issue Jan 22, 2024 · 1 comment
Closed

ObsoleteAttribute is ignored on nested complex types #2760

mike-loux-planview opened this issue Jan 22, 2024 · 1 comment

Comments

@mike-loux-planview
Copy link

I have an complex object with some properties I wish to mark as Deprecated in the Swagger UI.

Using the [Obsolete] attribute on simple properties (int, string, bool, DateTime, etc) works perfectly - the property appears with a line through it in the Schema section of the Swagger UI, and I can include a message in the <summary> XML doc comment that shows up as well.

However, this does not work for referenced complex types - the XML doc commands and attributes from the referenced object are shown instead, and the property is not shown to be deprecated.

Please see the attached simple sample project that reproduces this issue.

  • .NET Version - 7.0.15
  • Swashbuckle.AspNetCore version - 6.5.0

Code for the object in question:

/// <summary>
/// Object representing a person
/// </summary>
public class Person
{
  /// <summary>
  /// Person's first name
  /// </summary>
  public string? FirstName { get; set; }

  /// <summary>
  /// Person's middle name or initial
  /// </summary>
  public string? MiddleName { get; set; }
  
  /// <summary>
  /// Person's last name
  /// </summary>
  public string? LastName { get; set; }

  /// <summary>
  /// Person's suffix (e.g. Jr, III, etc)
  /// </summary>
  public string? Suffix { get; set; }

  /// <summary>
  /// Please use Suffix instead.
  /// </summary>
  [Obsolete]
  public bool? IsJunior { get; set; }

  /// <summary>
  /// Person's home address
  /// </summary>
  public Address? HomeAddress { get; set; }
  
  /// <summary>
  /// Please use AlternateAddress instead
  /// </summary>
  [Obsolete]
  public Address? WorkAddress { get; set; }
  
  /// <summary>
  /// Person's work or secondary address
  /// </summary>
  public Address? AlternateAddress { get; set; }
}

Screencap from running the above in Swagger:
image

@mike-loux-planview
Copy link
Author

Oh, interesting. Based on the previous issue I linked to, above, I had ruled out the use of UseAllOfToExtendReferenceSchemas option. However, when I tried it in my sample project, it did exactly what I needed.
So, adding...

builder.Services.AddSwaggerGen(options =>
{
  var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
  options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
  options.UseAllOfToExtendReferenceSchemas(); // This line right here
});

produced the following in my Swagger HTML:
image
That's perfect, actually. Sorry I doubted y'all.
Gonna close this as WAD.

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

1 participant