org.openapi4j.parser.validation.v3.RequestBodyValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-parser Show documentation
Show all versions of openapi-parser Show documentation
openapi4j schema parser & validator
package org.openapi4j.parser.validation.v3;
import org.openapi4j.core.validation.ValidationResult;
import org.openapi4j.core.validation.ValidationResults;
import org.openapi4j.parser.model.v3.MediaType;
import org.openapi4j.parser.model.v3.OpenApi3;
import org.openapi4j.parser.model.v3.RequestBody;
import org.openapi4j.parser.validation.ValidationContext;
import org.openapi4j.parser.validation.Validator;
import java.util.Map;
import static org.openapi4j.core.validation.ValidationSeverity.ERROR;
import static org.openapi4j.parser.validation.v3.OAI3Keywords.$REF;
import static org.openapi4j.parser.validation.v3.OAI3Keywords.CONTENT;
import static org.openapi4j.parser.validation.v3.OAI3Keywords.EXTENSIONS;
class RequestBodyValidator extends Validator3Base {
private static final ValidationResult ENCODING_MISMATCH = new ValidationResult(ERROR, 131, "The encoding object SHALL only apply to requestBody objects when the media type is multipart or application/x-www-form-urlencoded");
private static final String MULTIPART = "multipart/";
private static final String FORM_URL_ENCODED = "application/x-www-form-urlencoded";
private static final Validator INSTANCE = new RequestBodyValidator();
private RequestBodyValidator() {
}
public static Validator instance() {
return INSTANCE;
}
@Override
public void validate(ValidationContext context, OpenApi3 api, RequestBody requestBody, ValidationResults results) {
// VALIDATION EXCLUSIONS :
// description, required
if (requestBody.isRef()) {
validateReference(context, api, requestBody, results, $REF, RequestBodyValidator.instance(), RequestBody.class);
} else {
validateMap(context, api, requestBody.getContentMediaTypes(), results, false, CONTENT, Regexes.NOEXT_REGEX, MediaTypeValidator.instance());
validateMap(context, api, requestBody.getExtensions(), results, false, EXTENSIONS, Regexes.EXT_REGEX, null);
checkAllowedEncoding(requestBody, results);
}
}
private void checkAllowedEncoding(RequestBody requestBody, ValidationResults results) {
Map mediaTypes = requestBody.getContentMediaTypes();
if (mediaTypes == null) {
return;
}
for (Map.Entry entry : mediaTypes.entrySet()) {
MediaType mediaType = entry.getValue();
if (
mediaType.getEncodings() != null
&& !entry.getKey().startsWith(MULTIPART)
&& !entry.getKey().equals(FORM_URL_ENCODED)) {
results.add(ENCODING_MISMATCH, entry.getKey());
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy