Validation improvements for version upload via API

This commit is contained in:
Nassim Jahnke 2023-01-25 22:51:47 +01:00
parent 972197bbf3
commit 1c06cac69f
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
3 changed files with 14 additions and 8 deletions

View File

@ -2,9 +2,9 @@ package io.papermc.hangar.model.api.project.version;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.papermc.hangar.controller.validations.AtLeastOneNotNull;
import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.Named;
import io.papermc.hangar.model.api.project.ProjectNamespace;
import jakarta.validation.constraints.NotBlank;
import java.util.Objects;
import org.jdbi.v3.core.mapper.Nested;
import org.jdbi.v3.core.mapper.reflect.JdbiConstructor;
@ -13,7 +13,6 @@ import org.jetbrains.annotations.Nullable;
@AtLeastOneNotNull(fieldNames = {"namespace", "externalUrl"}, includeBlankStrings = true, message = "Must specify a projectId or external URL for a dependency")
public class PluginDependency implements Named {
@NotBlank(message = "Must have a dependency name")
private final String name;
private final boolean required;
private final ProjectNamespace namespace;
@ -21,8 +20,11 @@ public class PluginDependency implements Named {
@JdbiConstructor
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public PluginDependency(final String name, final boolean required, @Nested("pn") final @Nullable ProjectNamespace namespace, final String externalUrl) {
this.name = name;
public PluginDependency(final @Nullable String name, final boolean required, @Nested("pn") final @Nullable ProjectNamespace namespace, final String externalUrl) {
if (name == null && namespace == null) {
throw new HangarApiException("Must have a dependency name or a Hangar project namespace");
}
this.name = namespace != null ? null : name;
this.required = required;
this.namespace = namespace;
this.externalUrl = externalUrl;
@ -37,7 +39,7 @@ public class PluginDependency implements Named {
@Override
public String getName() {
return this.name;
return this.namespace != null ? this.namespace.getSlug() : this.name;
}
public boolean isRequired() {

View File

@ -8,6 +8,7 @@ import java.util.*;
import io.papermc.hangar.controller.validations.Validate;
import io.papermc.hangar.model.api.project.version.PluginDependency;
import io.papermc.hangar.model.common.Platform;
import org.checkerframework.checker.nullness.qual.Nullable;
public class VersionUpload {
@ -19,7 +20,6 @@ public class VersionUpload {
private final Map<Platform, @Size(min = 1, message = "version.edit.error.noPlatformVersions") SortedSet<@NotBlank(message = "version.new.error.invalidPlatformVersion") String>> platformDependencies;
// @el(root: String)
@NotBlank(message = "version.new.error.noDescription")
private final @Validate(SpEL = "@validate.max(#root, @hangarConfig.pages.maxLen)", message = "page.new.error.maxLength") String description;
@Size(min = 1, max = 3, message = "version.new.error.invalidNumOfPlatforms")
private final List<@Valid MultipartFileOrUrl> files;
@ -29,11 +29,11 @@ public class VersionUpload {
private final @Validate(SpEL = "@validate.regex(#root, @hangarConfig.channels.nameRegex)", message = "channel.modal.error.invalidName") String channel;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public VersionUpload(final String version, final Map<Platform, Set<PluginDependency>> pluginDependencies, final EnumMap<Platform, SortedSet<String>> platformDependencies, final String description, final List<MultipartFileOrUrl> files, final String channel) {
public VersionUpload(final String version, final Map<Platform, Set<PluginDependency>> pluginDependencies, final EnumMap<Platform, SortedSet<String>> platformDependencies, final @Nullable String description, final List<MultipartFileOrUrl> files, final String channel) {
this.version = version;
this.pluginDependencies = pluginDependencies;
this.platformDependencies = platformDependencies;
this.description = description;
this.description = description != null ? description : "*No description provided*";
this.files = files;
this.channel = channel;
}

View File

@ -139,6 +139,10 @@ public class VersionFactory extends HangarComponent {
this.createPendingUrl(channel, projectTable, pluginDependencies, platformDependencies, fileOrUrl);
}
} else {
if (files == null || files.isEmpty()) {
throw new HangarApiException("Missing a file for platform(s): " + fileOrUrl.platforms());
}
versionString = this.createPendingFile(files.remove(0), channel, projectTable, pluginDependencies, platformDependencies, pendingFiles, versionString, userTempDir, fileOrUrl, prefillDependencies);
}
}