From b3e9fd1003c41c666c2fecffe89ba198d1753fde Mon Sep 17 00:00:00 2001 From: Eric Wittmann Date: Tue, 14 May 2024 19:56:56 -0400 Subject: [PATCH] Extract metadata only in v2 rest api --- .../registry/rest/v2/GroupsResourceImpl.java | 36 +++++++++++++++---- .../apicurio/registry/rest/v2/V2ApiUtil.java | 24 ++++++------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/io/apicurio/registry/rest/v2/GroupsResourceImpl.java b/app/src/main/java/io/apicurio/registry/rest/v2/GroupsResourceImpl.java index ec0e60bce3..53e7e5ea1e 100644 --- a/app/src/main/java/io/apicurio/registry/rest/v2/GroupsResourceImpl.java +++ b/app/src/main/java/io/apicurio/registry/rest/v2/GroupsResourceImpl.java @@ -7,6 +7,8 @@ import io.apicurio.registry.auth.AuthorizedLevel; import io.apicurio.registry.auth.AuthorizedStyle; import io.apicurio.registry.content.ContentHandle; +import io.apicurio.registry.content.extract.ContentExtractor; +import io.apicurio.registry.content.extract.ExtractedMetaData; import io.apicurio.registry.metrics.health.liveness.ResponseErrorLivenessCheck; import io.apicurio.registry.metrics.health.readiness.ResponseTimeoutReadinessCheck; import io.apicurio.registry.model.BranchId; @@ -64,6 +66,7 @@ import io.apicurio.registry.types.ReferenceType; import io.apicurio.registry.types.RuleType; import io.apicurio.registry.types.VersionState; +import io.apicurio.registry.types.provider.ArtifactTypeUtilProvider; import io.apicurio.registry.util.ArtifactIdGenerator; import io.apicurio.registry.util.ArtifactTypeUtil; import io.apicurio.registry.util.ContentTypeUtil; @@ -557,7 +560,7 @@ public void testUpdateArtifact(String groupId, String artifactId, InputStream da } /** - * @see io.apicurio.registry.rest.v2.GroupsResource#getArtifactVersion(java.lang.String, java.lang.String, java.lang.String, io.apicurio.registry.rest.v2.beans.HandleReferencesType) + * @see io.apicurio.registry.rest.v2.GroupsResource#getArtifactVersion(String, String, String, Boolean) */ @Override @Authorized(style = AuthorizedStyle.GroupAndArtifact, level = AuthorizedLevel.Read) @@ -949,6 +952,7 @@ private ArtifactMetaData createArtifactWithRefs(String groupId, String xRegistry if (ContentTypeUtil.isApplicationYaml(ct) || (ContentTypeUtil.isApplicationCreateExtended(ct) && ContentTypeUtil.isParsableYaml(content))) { content = ContentTypeUtil.yamlToJson(content); + ct = ContentTypes.APPLICATION_JSON; } String artifactType = ArtifactTypeUtil.determineArtifactType(content, xRegistryArtifactType, ct, factory.getAllArtifactTypes()); @@ -961,8 +965,15 @@ private ArtifactMetaData createArtifactWithRefs(String groupId, String xRegistry rulesService.applyRules(defaultGroupIdToNull(groupId), artifactId, artifactType, content, RuleApplicationType.CREATE, toV3Refs(references), resolvedReferences); - final String finalArtifactId = artifactId; - EditableArtifactMetaDataDto metaData = getEditableArtifactMetaData(artifactName, artifactDescription); + + // Extract metadata from content + EditableArtifactMetaDataDto metaData = extractMetaData(artifactType, content); + if (artifactName != null && artifactName.trim().isEmpty()) { + metaData.setName(artifactName); + } + if (artifactDescription != null && artifactDescription.trim().isEmpty()) { + metaData.setDescription(artifactDescription); + } ContentWrapperDto contentDto = ContentWrapperDto.builder() .contentType(ct) @@ -970,8 +981,8 @@ private ArtifactMetaData createArtifactWithRefs(String groupId, String xRegistry .references(referencesAsDtos) .build(); EditableVersionMetaDataDto versionMetaData = EditableVersionMetaDataDto.builder() - .name(artifactName) - .description(artifactDescription) + .name(metaData.getName()) + .description(metaData.getDescription()) .labels(Map.of()) .build(); @@ -979,8 +990,7 @@ private ArtifactMetaData createArtifactWithRefs(String groupId, String xRegistry defaultGroupIdToNull(groupId), artifactId, artifactType, metaData, xRegistryVersion, contentDto, versionMetaData, List.of()); - ArtifactMetaDataDto amdDto = createResult.getKey(); - return V2ApiUtil.dtoToMetaData(amdDto.getArtifactId(), amdDto.getGroupId(), artifactType, amdDto); + return V2ApiUtil.dtoToMetaData(groupId, artifactId, artifactType, createResult.getRight()); } catch (ArtifactAlreadyExistsException ex) { return handleIfExists(groupId, xRegistryArtifactId, xRegistryVersion, ifExists, artifactName, artifactDescription, content, ct, fcanonical, references); } @@ -1247,4 +1257,16 @@ private static io.apicurio.registry.rest.v3.beans.ArtifactReference toV3Ref(Arti .build(); } + protected EditableArtifactMetaDataDto extractMetaData(String artifactType, ContentHandle content) { + ArtifactTypeUtilProvider provider = factory.getArtifactTypeProvider(artifactType); + ContentExtractor extractor = provider.getContentExtractor(); + ExtractedMetaData emd = extractor.extract(content); + EditableArtifactMetaDataDto metaData; + if (emd != null) { + metaData = new EditableArtifactMetaDataDto(emd.getName(), emd.getDescription(), null, emd.getLabels()); + } else { + metaData = new EditableArtifactMetaDataDto(); + } + return metaData; + } } diff --git a/app/src/main/java/io/apicurio/registry/rest/v2/V2ApiUtil.java b/app/src/main/java/io/apicurio/registry/rest/v2/V2ApiUtil.java index e5919a18ee..9047ae6c29 100644 --- a/app/src/main/java/io/apicurio/registry/rest/v2/V2ApiUtil.java +++ b/app/src/main/java/io/apicurio/registry/rest/v2/V2ApiUtil.java @@ -1,13 +1,5 @@ package io.apicurio.registry.rest.v2; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - import io.apicurio.registry.rest.v2.beans.ArtifactMetaData; import io.apicurio.registry.rest.v2.beans.ArtifactReference; import io.apicurio.registry.rest.v2.beans.ArtifactSearchResults; @@ -31,6 +23,14 @@ import io.apicurio.registry.storage.dto.VersionSearchResultsDto; import io.apicurio.registry.types.ArtifactState; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + public final class V2ApiUtil { private V2ApiUtil() { @@ -138,7 +138,7 @@ public static Map toV3Labels(List v2Labels, Map