Skip to content

Commit

Permalink
Extract metadata only in v2 rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed May 14, 2024
1 parent ade82a0 commit b3e9fd1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand All @@ -961,26 +965,32 @@ 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)
.content(content)
.references(referencesAsDtos)
.build();
EditableVersionMetaDataDto versionMetaData = EditableVersionMetaDataDto.builder()
.name(artifactName)
.description(artifactDescription)
.name(metaData.getName())
.description(metaData.getDescription())
.labels(Map.of())
.build();

Pair<ArtifactMetaDataDto, ArtifactVersionMetaDataDto> createResult = storage.createArtifact(
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);
}
Expand Down Expand Up @@ -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;
}
}
24 changes: 12 additions & 12 deletions app/src/main/java/io/apicurio/registry/rest/v2/V2ApiUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -138,7 +138,7 @@ public static Map<String, String> toV3Labels(List<String> v2Labels, Map<String,
* @param artifactType
* @param dto
*/
public static final ArtifactMetaData dtoToMetaData(String groupId, String artifactId, String artifactType,
public static ArtifactMetaData dtoToMetaData(String groupId, String artifactId, String artifactType,
ArtifactVersionMetaDataDto dto) {
ArtifactMetaData metaData = new ArtifactMetaData();
metaData.setCreatedBy(dto.getOwner());
Expand Down Expand Up @@ -172,7 +172,7 @@ public static final ArtifactMetaData dtoToMetaData(String groupId, String artifa
* @param artifactType
* @param dto
*/
public static final VersionMetaData dtoToVersionMetaData(String groupId, String artifactId,
public static VersionMetaData dtoToVersionMetaData(String groupId, String artifactId,
String artifactType, ArtifactMetaDataDto dto) {
VersionMetaData metaData = new VersionMetaData();
metaData.setGroupId(groupId);
Expand All @@ -195,7 +195,7 @@ public static final VersionMetaData dtoToVersionMetaData(String groupId, String
* @param artifactType
* @param dto
*/
public static final VersionMetaData dtoToVersionMetaData(String groupId, String artifactId, String artifactType,
public static VersionMetaData dtoToVersionMetaData(String groupId, String artifactId, String artifactType,
ArtifactVersionMetaDataDto dto) {
VersionMetaData metaData = new VersionMetaData();
metaData.setGroupId(groupId);
Expand All @@ -221,7 +221,7 @@ public static final VersionMetaData dtoToVersionMetaData(String groupId, String
* @param editableArtifactMetaData
* @return the updated ArtifactMetaDataDto object
*/
public static final ArtifactMetaDataDto setEditableMetaDataInArtifact(ArtifactMetaDataDto amdd, EditableArtifactMetaDataDto editableArtifactMetaData) {
public static ArtifactMetaDataDto setEditableMetaDataInArtifact(ArtifactMetaDataDto amdd, EditableArtifactMetaDataDto editableArtifactMetaData) {
if (editableArtifactMetaData.getName() != null) {
amdd.setName(editableArtifactMetaData.getName());
}
Expand Down

0 comments on commit b3e9fd1

Please sign in to comment.