SwaggerValidatorUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.openapi.validator.swagger;

  21. import java.io.IOException;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.Optional;
  24. import java.util.regex.Pattern;

  25. import com.atlassian.oai.validator.model.Body;
  26. import com.atlassian.oai.validator.report.MessageResolver;
  27. import com.atlassian.oai.validator.report.ValidationReport;
  28. import com.atlassian.oai.validator.schema.SchemaValidator;
  29. import com.fasterxml.jackson.databind.JsonNode;
  30. import com.google.common.base.CharMatcher;

  31. import io.swagger.v3.oas.models.media.Schema;

  32. /**
  33.  * SwaggerValidatorUtils
  34.  *
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  *
  38.  */
  39. public class SwaggerValidatorUtils {

  40.     public static boolean isBase64SchemaFile(Schema<?> schema) {
  41.         return "string".equals(schema.getType()) && "base64".equals(schema.getFormat());
  42.     }

  43.     public static boolean isBinarySchemaFile(Schema<?> schema) {
  44.         return "string".equals(schema.getType()) && "binary".equals(schema.getFormat());
  45.     }

  46.     // Adattata dalla com.github.fgce...jsonschema
  47.     private static final Optional<String> validateBase64String(String input) {
  48.        
  49.         /*
  50.          * Regex to accurately remove _at most two_ '=' characters from the end of the
  51.          * input.
  52.          */
  53.         final Pattern PATTERN = Pattern.compile("==?$");
  54.    
  55.         /*
  56.          * Negation of the Base64 alphabet. We try and find one character, if any,
  57.          * matching this "negated" character matcher.
  58.          *
  59.          * FIXME: use .precomputed()?
  60.          */
  61.         final CharMatcher NOT_BASE64 = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'))
  62.                 .or(CharMatcher.inRange('0', '9')).or(CharMatcher.anyOf("+/")).negate();
  63.            
  64.         if (input.length() % 4 != 0) {
  65.             return Optional.of("err.format.base64.badLength, should be multiple of 4: " + input.length());
  66.         }
  67.    
  68.         final int index = NOT_BASE64.indexIn(PATTERN.matcher(input).replaceFirst(""));
  69.    
  70.         if (index == -1)
  71.             return Optional.empty();
  72.    
  73.         return Optional.of(
  74.                 "err.format.base64.illegalChars: character '" + Character.toString(input.charAt(index)) + "' index " + index);          
  75.     }
  76.     public static final ValidationReport validateBase64Body(Body body, MessageResolver messages, boolean request) {
  77.         try {      
  78.             var error = SwaggerValidatorUtils.validateBase64String(body.toString(StandardCharsets.UTF_8));
  79.             if (error.isPresent()) {
  80.                 return ValidationReport.singleton(
  81.                         messages.create(request ? "validation.request.body.schema": "validation.response.body.schema",
  82.                                 (request ? "[REQUEST] ": "[RESPONSE] ")+
  83.                                         error.get()));
  84.             } else {
  85.                 return ValidationReport.empty();
  86.             }
  87.         } catch (IOException e) {
  88.             throw new RuntimeException(e);
  89.         }
  90.     }
  91.    
  92.     public static final ValidationReport validateJsonFormat(Body body, MessageResolver messages, boolean request) {
  93.         try {
  94.             body.toJsonNode();
  95.         } catch (final IOException e) {
  96.             return ValidationReport.singleton(
  97.                     messages.create(
  98.                             request ? "validation.request.body.schema.invalidJson" : "validation.response.body.schema.invalidJson",
  99.                             messages.get(SchemaValidator.INVALID_JSON_KEY, e.getMessage()).getMessage()
  100.                     )
  101.             );
  102.         }
  103.         return ValidationReport.empty();        
  104.     }

  105.     public static final String getSchemaVersion(JsonNode node) {
  106.         if (node == null) {
  107.             return null;
  108.         }
  109.    
  110.         JsonNode version = node.get("openapi");
  111.         if (version != null) {
  112.             return version.toString();
  113.         }
  114.    
  115.         version = node.get("swagger");
  116.         if (version != null) {
  117.             return version.toString();
  118.         }
  119.         version = node.get("swaggerVersion");
  120.         if (version != null) {
  121.             return version.toString();
  122.         }
  123.    
  124.         return null;
  125.     }
  126.    
  127.     public static final boolean isSchemaV1(String version) {
  128.         return version != null && (version.startsWith("\"1") || version.startsWith("1"));
  129.     }
  130.    
  131.     public static final boolean isSchemaV2(String version) {
  132.         return version != null && (version.startsWith("\"2") || version.startsWith("2"));      
  133.     }
  134.    
  135.     public static final boolean isSchemaV3(String version) {
  136.         return version != null && (version.startsWith("\"3") || version.startsWith("3"));
  137.        
  138.     }

  139. }