Skip to content

Commit

Permalink
Fix OpenAPI schema, mark unused API as obsolete (#653)
Browse files Browse the repository at this point in the history
* Fix OpenAPI schema, mark unused API as obsolete

* Fix code smell

---------

Co-authored-by: Oleg Zhuk <[email protected]>
  • Loading branch information
asvishnyakov and OlegoO authored Sep 18, 2023
1 parent 29c42f3 commit f88832e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 26 deletions.
11 changes: 11 additions & 0 deletions VirtoCommerce.Storefront.Model/SlugInfoRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace VirtoCommerce.Storefront.Model;

public class SlugInfoRequest
{
public string CultureName { get; set; }

[Required]
public string Slug { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace VirtoCommerce.Storefront.Model.StaticContent
{
public class ContentInThemeSearchCriteria
{
public string Permalink { get; set; }
public string Template { get; set; }

[Obsolete("Use TemplateName instead")]
public string Template
{
get => TemplateName;
set => TemplateName = value;
}

[Required]
public string TemplateName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public ApiAccountController(IWorkContextAccessor workContextAccessor,
}

// GET: storefrontapi/account
[Obsolete("Use me query from GraphQL")]
[HttpGet]
[AllowAnonymous]
public ActionResult<User> GetCurrentUser()
Expand Down Expand Up @@ -156,6 +157,7 @@ private void ResetIdentityCookies()
}

// POST: storefrontapi/account/user
[Obsolete("Use requestRegistration mutations from GraphQL")]
[HttpPost("user")]
public async Task<ActionResult<UserActionIdentityResult>> RegisterUser([FromBody] UserRegistration registration)
{
Expand Down Expand Up @@ -228,6 +230,7 @@ public async Task<ActionResult<UserActionIdentityResult>> RegisterUser([FromBody


// POST: storefrontapi/account/password
[Obsolete("Use changePassword mutation from GraphQL")]
[HttpPost("password")]
public async Task<ActionResult<PasswordChangeResult>> ChangePassword([FromBody] ChangePassword formModel)
{
Expand Down Expand Up @@ -309,6 +312,7 @@ public async Task<ActionResult> Logout()
}

// POST: storefrontapi/account/forgotpassword
[Obsolete("Use requestPasswordReset mutation from GraphQL")]
[HttpPost("forgotPassword")]
[AllowAnonymous]
public async Task<ActionResult<UserActionIdentityResult>> ForgotPassword([FromBody] ForgotPasswordModel forgotPassword)
Expand Down Expand Up @@ -381,6 +385,7 @@ public async Task<ActionResult<UserActionIdentityResult>> ForgotPassword([FromBo
}

// POST: storefrontapi/account/validateToken
[Obsolete("This API no longer used. See XAPI documentation")]
[HttpPost("validateToken")]
[AllowAnonymous]
public async Task<ActionResult<UserActionIdentityResult>> ValidateResetPasswordToken([FromBody] ValidateTokenModel model)
Expand Down Expand Up @@ -415,6 +420,7 @@ public async Task<ActionResult<UserActionIdentityResult>> ValidateResetPasswordT
}

// POST: storefrontapi/account/resetPassword
[Obsolete("Use resetPasswordByToken mutation from GraphQL")]
[HttpPost("resetPassword")]
[AllowAnonymous]
public async Task<ActionResult<UserActionIdentityResult>> ResetPassword([FromBody] ResetPasswordModel model)
Expand Down Expand Up @@ -455,6 +461,7 @@ public async Task<ActionResult<UserActionIdentityResult>> ResetPassword([FromBod
}

// POST: storefrontapi/account/confirmemail
[Obsolete("Use confirmEmail mutation from GraphQL")]
[HttpPost("confirmemail")]
[AllowAnonymous]
public async Task<ActionResult<UserActionIdentityResult>> ConfirmEmail([FromBody] ConfirmEmailModel model)
Expand Down
32 changes: 24 additions & 8 deletions VirtoCommerce.Storefront/Controllers/Api/ApiCommonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public ApiCommonController(
}

// GET: storefrontapi/countries
[Obsolete("Use countries query from GraphQL")]
[HttpGet("countries")]
public ActionResult<Country[]> GetCountries()
{
return _countriesWithoutRegions;
}

// GET: storefrontapi/countries/{countryCode}/regions
[Obsolete("Use countries or regions query from GraphQL")]
[HttpGet("countries/{countryCode}/regions")]
public ActionResult<CountryRegion[]> GetCountryRegions(string countryCode)
{
Expand All @@ -61,9 +63,31 @@ public async Task<ActionResult> Feedback([FromBody] ContactForm model)

return Ok();
}

[HttpPost("slug")]
public async Task<SlugInfoResult> GetSlugInfoBySlugAsync([FromBody] SlugInfoRequest slugInfoRequest)
{
return await GetSlugInfoAsync(slugInfoRequest.Slug, slugInfoRequest.CultureName);
}

// Wildcard parameters are not supported by OpenAPI (including latest 3.1)
// https://stackoverflow.com/a/42880107/507434
[Obsolete("Use GetSlugInfoBySlug (POST /slug) instead")]
[HttpGet("slug/{*slug}")]
public async Task<SlugInfoResult> GetInfoBySlugAsync(string slug, [FromQuery] string culture)
{
return await GetSlugInfoAsync(slug, culture);
}

// GET: storefrontapi/version
[HttpGet("version")]
[AllowAnonymous]
public ActionResult Version()
{
return Ok(System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location).FileVersion);
}

private async Task<SlugInfoResult> GetSlugInfoAsync(string slug, string culture)
{
var result = new SlugInfoResult();

Expand Down Expand Up @@ -102,13 +126,5 @@ public async Task<SlugInfoResult> GetInfoBySlugAsync(string slug, [FromQuery] st

return result;
}

// GET: storefrontapi/version
[HttpGet("version")]
[AllowAnonymous]
public ActionResult Version()
{
return Ok(System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location).FileVersion);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VirtoCommerce.LiquidThemeEngine;
using VirtoCommerce.Storefront.Domain;
Expand Down Expand Up @@ -35,7 +35,7 @@ public ActionResult ResetCache([FromBody] ResetCacheEventModel webHookEvent)

// POST: storefrontapi/content/reset-cache/theme
[HttpPost("reset-cache/{region}")]
public ActionResult ResetCache([FromRoute] string region)
public ActionResult ResetCacheRegion([FromRoute] string region)
{
if (TryResetCacheInternal(region))
{
Expand Down Expand Up @@ -63,7 +63,9 @@ private static bool TryResetCacheInternal(string region)

// POST: storefrontapi/content/pages
[HttpPost("pages")]
public ActionResult FindPage([FromBody]ContentInThemeSearchCriteria value)
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(object))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult FindPage([FromBody] ContentInThemeSearchCriteria value)
{
var permalink = value.Permalink;
var result = WorkContext.Pages.FirstOrDefault(x => x.Permalink != null && x.Permalink.EqualsInvariant(permalink));
Expand All @@ -75,10 +77,11 @@ public ActionResult FindPage([FromBody]ContentInThemeSearchCriteria value)
}

[HttpPost("templates")]
public ActionResult LoadTemplate([FromBody]ContentInThemeSearchCriteria value)
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(object))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult LoadTemplate([FromBody] ContentInThemeSearchCriteria value)
{
var template = value.Template;
var result = WorkContext.Templates.FirstOrDefault(x => x.Name == template);
var result = WorkContext.Templates.FirstOrDefault(x => x.Name == value.TemplateName);
if (result == null)
{
return NotFound();
Expand All @@ -88,7 +91,7 @@ public ActionResult LoadTemplate([FromBody]ContentInThemeSearchCriteria value)

private ActionResult JsonResult(string content)
{
return Content(content, "application/json");
return Content(content, MediaTypeNames.Application.Json);
}
}
}
3 changes: 3 additions & 0 deletions VirtoCommerce.Storefront/Controllers/CommonController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -82,6 +83,7 @@ public async Task<ActionResult> SetCurrency(string currency, string returnUrl =
}

// GET: common/getcountries/json
[Obsolete("Use countries query from GraphQL")]
[HttpGet("common/getcountries/json")]
public ActionResult GetCountries()
{
Expand All @@ -90,6 +92,7 @@ public ActionResult GetCountries()
}

// GET: common/getregions/{countryCode}/json
[Obsolete("Use countries or regions query from GraphQL")]
[HttpGet("common/getregions/{countryCode}/json")]
public ActionResult GetRegions(string countryCode)
{
Expand Down
2 changes: 1 addition & 1 deletion VirtoCommerce.Storefront/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,14 @@ public void ConfigureServices(IServiceCollection services)
c.OperationFilter<ArrayInQueryParametersFilter>();
c.OperationFilter<FileUploadOperationFilter>();
c.SchemaFilter<EnumSchemaFilter>();
c.SchemaFilter<NewtonsoftJsonIgnoreFilter>();

// Use method name as operation ID, i.e. ApiAccount.GetOrganization instead of /storefrontapi/account/organization (will be treated as just organization method)
c.CustomOperationIds(apiDesc => apiDesc.TryGetMethodInfo(out var methodInfo) ? methodInfo.Name : null);

// To avoid errors with repeating type names
c.CustomSchemaIds(type => (Attribute.GetCustomAttribute(type, typeof(SwaggerSchemaIdAttribute)) as SwaggerSchemaIdAttribute)?.Id ?? type.FriendlyId());
});
services.AddSwaggerGenNewtonsoftSupport();

services.AddResponseCompression();

Expand Down
9 changes: 5 additions & 4 deletions VirtoCommerce.Storefront/VirtoCommerce.Storefront.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@
<PackageReference Include="ProxyKit" Version="2.3.4" />
<PackageReference Include="Scrutor" Version="4.1.0" />
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.1" />
<PackageReference Include="VirtoCommerce.Tools" Version="3.200.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
<PackageReference Include="VirtoCommerce.Tools" Version="3.400.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

Expand Down

0 comments on commit f88832e

Please sign in to comment.