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

Schema generation resiliency improvements #49

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.VisualStudio.JavaScript.Sdk/0.5.126-alpha">
<PropertyGroup>
<StartupCommand>npm run dev</StartupCommand>
<CleanCommand>npm run clean</CleanCommand>
<ShouldRunNpmBuildScript>false</ShouldRunNpmBuildScript>
<StaticWebAssetSourceId>Umbraco.Community.DeliveryApiExtensions</StaticWebAssetSourceId>
<DebugAssetsDirectory>$(MSBuildProjectDirectory)\dist</DebugAssetsDirectory>
Expand Down
1 change: 1 addition & 0 deletions src/UmbracoDeliveryApiExtensions.UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev:clean": "vite --force --base /",
"build": "tsc && vite build",
"watch": "chokidar src public -c \"tsc && vite build\" --initial",
"clean": "node -e \"['dist', 'obj', 'node_modules'].forEach(f => require('node:fs').rmSync(f, { recursive: true, force: true }))\"",
"postinstall": "patch-package"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public class ContentTypePropertyInfo
/// </summary>
public required string Alias { get; set; }

/// <summary>
/// Property Editor alias.
/// </summary>
public required string EditorAlias { get; set; }

/// <summary>
/// Property delivery api type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ICollection<ContentTypeInfo> GetContentTypes()
Alias = contentType.Alias,
SchemaId = GetContentTypeSchemaId(contentType),
CompositionSchemaIds = contentType.ContentTypeComposition.Select(GetContentTypeSchemaId).ToList(),
Properties = publishedContentType.PropertyTypes.Select(p => new ContentTypePropertyInfo { Alias = p.Alias, Type = p.DeliveryApiModelClrType, Inherited = !ownPropertyAliases.Contains(p.Alias) }).ToList(),
Properties = publishedContentType.PropertyTypes.Select(p => new ContentTypePropertyInfo { Alias = p.Alias, EditorAlias = p.EditorAlias, Type = p.DeliveryApiModelClrType, Inherited = !ownPropertyAliases.Contains(p.Alias) }).ToList(),
IsElement = contentType.IsElement,
IsComposition = false,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand All @@ -17,18 +18,21 @@ public class DeliveryApiContentTypesSchemaFilter : ISchemaFilter, IDocumentFilte
private readonly IOptionsMonitor<TypedSwaggerOptions> _typedSwaggerOptions;
private readonly IContentTypeInfoService _contentTypeInfoService;
private readonly ISchemaIdSelector _schemaIdSelector;
private readonly ILogger<DeliveryApiContentTypesSchemaFilter> _logger;

/// <summary>
/// Initializes a new instance of the <see cref="DeliveryApiContentTypesSchemaFilter" /> class.
/// </summary>
public DeliveryApiContentTypesSchemaFilter(
IOptionsMonitor<TypedSwaggerOptions> typedSwaggerOptions,
IContentTypeInfoService contentTypeInfoService,
ISchemaIdSelector schemaIdSelector)
ISchemaIdSelector schemaIdSelector,
ILogger<DeliveryApiContentTypesSchemaFilter> logger)
{
_typedSwaggerOptions = typedSwaggerOptions;
_contentTypeInfoService = contentTypeInfoService;
_schemaIdSelector = schemaIdSelector;
_logger = logger;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -242,7 +246,7 @@ private void ApplyPolymorphicContentTypeSchema<T>(OpenApiSchema schema, SchemaFi
}
}

private static OpenApiSchema ContentTypePropertiesMapper(ContentTypeInfo contentType, DocumentFilterContext context)
private OpenApiSchema ContentTypePropertiesMapper(ContentTypeInfo contentType, DocumentFilterContext context)
{
return context.SchemaRepository.AddDefinition(
$"{contentType.SchemaId}PropertiesModel",
Expand All @@ -260,8 +264,20 @@ private static OpenApiSchema ContentTypePropertiesMapper(ContentTypeInfo content
p => p.Alias,
p =>
{
OpenApiSchema propertySchema = context.SchemaGenerator.GenerateSchema(p.Type, context.SchemaRepository);
propertySchema.Nullable = true;
OpenApiSchema propertySchema;
try
{
propertySchema = context.SchemaGenerator.GenerateSchema(p.Type, context.SchemaRepository);
propertySchema.Nullable = true;
}
catch (Exception ex)
{
// Just default to any type in case of error
propertySchema = new OpenApiSchema();

_logger.LogWarning(ex, "Failed to generate {PropertyType} schema of {PropertyEditorAlias} for property {PropertyAlias} of content type {ContentTypeAlias}", p.Type, p.EditorAlias, p.Alias, contentType.Alias);
}

return propertySchema;
}
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<NoWarn>$(NoWarn),1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Umbraco.Cms" Version="13.0.1" />
<PackageReference Include="Umbraco.Cms" Version="13.5.2" />
<PackageReference Include="uSync" Version="13.2.5" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@
{
"name": "fetch",
"in": "query",
"description": "Specifies the media items to fetch. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Specifies the media items to fetch. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand All @@ -1343,7 +1343,7 @@
{
"name": "filter",
"in": "query",
"description": "Defines how to filter the fetched media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines how to filter the fetched media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "array",
"items": {
Expand All @@ -1369,7 +1369,7 @@
{
"name": "sort",
"in": "query",
"description": "Defines how to sort the found media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines how to sort the found media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "array",
"items": {
Expand Down Expand Up @@ -1429,7 +1429,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -1492,7 +1492,7 @@
{
"name": "fetch",
"in": "query",
"description": "Specifies the media items to fetch. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Specifies the media items to fetch. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand All @@ -1511,7 +1511,7 @@
{
"name": "filter",
"in": "query",
"description": "Defines how to filter the fetched media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines how to filter the fetched media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "array",
"items": {
Expand All @@ -1537,7 +1537,7 @@
{
"name": "sort",
"in": "query",
"description": "Defines how to sort the found media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines how to sort the found media items. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "array",
"items": {
Expand Down Expand Up @@ -1597,7 +1597,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand All @@ -1622,7 +1622,7 @@
{
"name": "fields",
"in": "query",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -1696,7 +1696,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -1760,7 +1760,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -1831,7 +1831,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand All @@ -1856,7 +1856,7 @@
{
"name": "fields",
"in": "query",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -1927,7 +1927,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -1999,7 +1999,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand All @@ -2024,7 +2024,7 @@
{
"name": "fields",
"in": "query",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -2098,7 +2098,7 @@
{
"name": "expand",
"in": "query",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Defines the properties that should be expanded in the response. Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand All @@ -2123,7 +2123,7 @@
{
"name": "fields",
"in": "query",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api#query-parameters) for more details on this.",
"description": "Explicitly defines which properties should be included in the response (by default all properties are included). Refer to [the documentation](https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/media-delivery-api#query-parameters) for more details on this.",
"schema": {
"type": "string"
},
Expand Down Expand Up @@ -2546,10 +2546,10 @@
"propertyName": "contentType",
"mapping": {
"blockSettings": "#/components/schemas/BlockSettingsElementModel",
"testComposition": "#/components/schemas/TestCompositionElementModel",
"testComposition2": "#/components/schemas/TestComposition2ElementModel",
"testBlock": "#/components/schemas/TestBlockElementModel",
"testBlock2": "#/components/schemas/TestBlock2ElementModel"
"testBlock2": "#/components/schemas/TestBlock2ElementModel",
"testComposition": "#/components/schemas/TestCompositionElementModel",
"testComposition2": "#/components/schemas/TestComposition2ElementModel"
}
}
},
Expand Down
Loading