io.swagger.models.parameters.SerializableParameter Java Examples

The following examples show how to use io.swagger.models.parameters.SerializableParameter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 6 votes vote down vote up
@Test()
public void testArrayMulti() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("array");
    parameter.setCollectionFormat("multi");
    List<String> paramsList = new ArrayList<>();
    paramsList.add("toto");
    paramsList.add("tutu");
    paramsList.add("titi");
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, paramsList);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertArrayEquals(paramsList.toArray(), ((List) result).toArray());
}
 
Example #2
Source File: SwaggerTypeAdapter.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
static SwaggerTypeAdapter create(Object swaggerType) {
  if (swaggerType instanceof SwaggerTypeAdapter) {
    return (SwaggerTypeAdapter) swaggerType;
  }

  if (swaggerType instanceof Model) {
    return new ModelAdapter((Model) swaggerType);
  }

  if (swaggerType instanceof Property) {
    return new PropertyAdapter((Property) swaggerType);
  }

  if (swaggerType instanceof SerializableParameter) {
    return new SerializableParameterAdapter((SerializableParameter) swaggerType);
  }

  if (swaggerType instanceof BodyParameter) {
    return new BodyParameterAdapter((BodyParameter) swaggerType);
  }

  throw new IllegalStateException("not support swagger type: " + swaggerType.getClass().getName());
}
 
Example #3
Source File: ValidatorTestUtil.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
public static SerializableParameter arrayParam(final boolean required,
                                               final String collectionFormat,
                                               final Integer minItems,
                                               final Integer maxItems,
                                               final Boolean unique,
                                               final Property items) {

    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("array");
    when(result.getCollectionFormat()).thenReturn(collectionFormat);
    when(result.getRequired()).thenReturn(required);
    when(result.getMinItems()).thenReturn(minItems);
    when(result.getMaxItems()).thenReturn(maxItems);
    when(result.isUniqueItems()).thenReturn(unique);
    when(result.getItems()).thenReturn(items);
    return result;
}
 
Example #4
Source File: AbstractSerializableParameterExtractor.java    From vertx-swagger with Apache License 2.0 6 votes vote down vote up
private String getArrayRegex(SerializableParameter param) {
    String regex;
    if (param.getCollectionFormat() == null) {
        regex = Pattern.quote(",");
    } else {
        switch (param.getCollectionFormat()) {
        case "ssv":
            regex = Pattern.quote(" ");
            break;
        case "csv":
            regex = Pattern.quote(",");
            break;
        case "tsv":
            regex = Pattern.quote("\t");
            break;
        case "pipes":
            regex = Pattern.quote("|");
            break;
        default:
            regex = Pattern.quote(",");
            break;
        }
    }
    return regex;
}
 
Example #5
Source File: ArrayParameterValidator.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Override
public Status validate(final String value, final Parameter p) {

    if (!supports(p)) {
        return null;
    }

    final SerializableParameter parameter = (SerializableParameter)p;

    if (parameter.getRequired() && (value == null || value.trim().isEmpty())) {
        return new Status("ERR11001", parameter.getName());
    }

    if (value == null || value.trim().isEmpty()) {
        return null;
    }

    return doValidate(value, parameter);
}
 
Example #6
Source File: ArrayParameterValidator.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
public Status validate(final Collection<String> values, final Parameter p) {
    if (p == null) {
        return null;
    }

    final SerializableParameter parameter = (SerializableParameter)p;
    if (parameter.getRequired() && (values == null || values.isEmpty())) {
        return new Status("ERR11001", parameter.getName());
    }

    if (values == null) {
        return null;
    }

    if (!parameter.getCollectionFormat().equalsIgnoreCase(CollectionFormat.MULTI.name())) {
        return new Status("ERR11005", p.getName(), parameter.getCollectionFormat(), "multi");
    }

    return doValidate(values, parameter);
}
 
Example #7
Source File: SwaggerSpecificationProcessor.java    From swagger-coverage with Apache License 2.0 6 votes vote down vote up
public static List<String> extractEnum(Parameter p) {
    List<String> enumValues = null;

    if (p instanceof SerializableParameter) {
        SerializableParameter serializableParameter = (SerializableParameter) p;
        if (serializableParameter.getEnum() != null && !serializableParameter.getEnum().isEmpty()) {
            enumValues = serializableParameter.getEnum();
        }

        if (enumValues == null && serializableParameter.getItems() != null) {
            Property items = serializableParameter.getItems();
            if (items instanceof StringProperty) {
                StringProperty stringProperty = (StringProperty) items;
                if (stringProperty.getEnum() != null && !stringProperty.getEnum().isEmpty()) {
                    enumValues = stringProperty.getEnum();
                }
            }
        }
    }

    return enumValues;
}
 
Example #8
Source File: ParameterValidators.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
public Status validate(final String value, final Parameter parameter) {
    requireNonNull(parameter);

    if ((parameter instanceof SerializableParameter) &&
            ((SerializableParameter)parameter).getType().equalsIgnoreCase("array")) {
        return arrayValidator.validate(value, parameter);
    }


    Optional<Status> optional = validators.stream()
            .filter(v -> v.supports(parameter))
            .map(v -> v.validate(value, parameter))
            .filter(s -> s != null)
            .findFirst();
    if(optional.isPresent()) {
        return optional.get();
    } else {
        return null;
    }
}
 
Example #9
Source File: BaseParameterValidator.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
@Override
public Status validate(final String value, final Parameter p) {

    if (!supports(p)) {
        return null;
    }

    final SerializableParameter parameter = (SerializableParameter)p;

    if (parameter.getRequired() && (value == null || value.trim().isEmpty())) {
        return new Status("ERR11001", p.getName());
    }

    if (value == null || value.trim().isEmpty()) {
        return null;
    }

    if (!matchesEnumIfDefined(value, parameter)) {
        return new Status("ERR11002", value, parameter.getName(), parameter.getEnum());
    }
    return doValidate(value, parameter);
}
 
Example #10
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testMissingRequiredParam() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setRequired(true);

    abstractSerializableParameterExtractor.extract(name, parameter,
            MultiMap.caseInsensitiveMultiMap());
}
 
Example #11
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testNotAllowEmptyValueWithEmptyParameter() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setAllowEmptyValue(false);
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, "");

    abstractSerializableParameterExtractor.extract(name, parameter, params);
}
 
Example #12
Source File: ValidatorTestUtil.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
public static SerializableParameter intParam(final boolean required, final BigDecimal min, final BigDecimal max) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("integer");
    when(result.getFormat()).thenReturn("int32");
    when(result.getRequired()).thenReturn(required);
    when(result.getMinimum()).thenReturn(min);
    when(result.getMaximum()).thenReturn(max);
    return result;
}
 
Example #13
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testMissingNonRequiredParam() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setRequired(false);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter,
            MultiMap.caseInsensitiveMultiMap());
    Assert.assertNull(result);
}
 
Example #14
Source File: ValidatorTestUtil.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
public static SerializableParameter stringParam(final boolean required) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("string");
    when(result.getRequired()).thenReturn(required);
    when(result.getMinimum()).thenReturn(null);
    when(result.getMaximum()).thenReturn(null);
    return result;
}
 
Example #15
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testArraySsv() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("array");
    parameter.setCollectionFormat("ssv");
    String param = "toto tutu titi";
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, param);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertArrayEquals(new String[] { "toto", "tutu", "titi" },
            ((List) result).toArray());
}
 
Example #16
Source File: ValidatorTestUtil.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
public static SerializableParameter floatParam(final boolean required, final BigDecimal min, final BigDecimal max) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("number");
    when(result.getFormat()).thenReturn("float");
    when(result.getRequired()).thenReturn(required);
    when(result.getMinimum()).thenReturn(min);
    when(result.getMaximum()).thenReturn(max);
    return result;
}
 
Example #17
Source File: AbstractSerializableParameterExtractor.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
private List<String> splitArrayParam(SerializableParameter param, String paramAsString) {
    String regex = getArrayRegex(param);
    try {
        return Arrays.asList(URLDecoder.decode(paramAsString, "UTF-8").split(regex));
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError("UTF-8 not supported");
    }
}
 
Example #18
Source File: ValidatorTestUtil.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
public static SerializableParameter enumeratedArrayParam(final boolean required,
                                                         final String collectionFormat,
                                                         final Property items,
                                                         final String... enumValues) {
    final SerializableParameter result = mock(SerializableParameter.class);
    when(result.getName()).thenReturn("Test Parameter");
    when(result.getType()).thenReturn("array");
    when(result.getCollectionFormat()).thenReturn(collectionFormat);
    when(result.getRequired()).thenReturn(required);
    when(result.getMaxItems()).thenReturn(null);
    when(result.getMinItems()).thenReturn(null);
    when(result.getEnum()).thenReturn(asList(enumValues));
    when(result.getItems()).thenReturn(items);
    return result;
}
 
Example #19
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testNullAllowEmptyValueWithEmptyParameter() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, "");

    abstractSerializableParameterExtractor.extract(name, parameter, params);
}
 
Example #20
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testArray() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("array");
    parameter.setCollectionFormat(null);
    String param = "toto,tutu,titi";
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, param);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertArrayEquals(new String[] { "toto", "tutu", "titi" },
            ((List) result).toArray());
}
 
Example #21
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testArrayCsv() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("array");
    parameter.setCollectionFormat("csv");
    String param = "toto,tutu,titi";
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, param);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertArrayEquals(new String[] { "toto", "tutu", "titi" },
            ((List) result).toArray());
}
 
Example #22
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testArrayTsv() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("array");
    parameter.setCollectionFormat("tsv");
    String param = "toto\ttutu\ttiti";
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, param);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertArrayEquals(new String[] { "toto", "tutu", "titi" },
            ((List) result).toArray());
}
 
Example #23
Source File: ArrayParameterValidator.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
private Status doValidate(final Collection<String> values,
                        final SerializableParameter parameter) {

    if (parameter.getMaxItems() != null && values.size() > parameter.getMaxItems()) {
        return new Status("ERR11006", parameter.getName(), parameter.getMaxItems(), values.size());
    }

    if (parameter.getMinItems() != null && values.size() < parameter.getMinItems()) {
        return new Status("ERR11007", parameter.getName(), parameter.getMinItems(), values.size());
    }

    if (Boolean.TRUE.equals(parameter.isUniqueItems()) &&
            values.stream().distinct().count() != values.size()) {
        return new Status("ERR11008", parameter.getName());
    }

    if (parameter.getEnum() != null && !parameter.getEnum().isEmpty()) {
        final Set<String> enumValues = new HashSet<>(parameter.getEnum());
        Optional<String> value =
            values.stream()
                .filter(v -> !enumValues.contains(v))
                .findFirst();
        if(value.isPresent()) {
            return new Status("ERR11009", value.get(), parameter.getName(), parameter.getEnum());
        }
    }

    Optional<Status> optional =
            values.stream()
            .map(v -> schemaValidator.validate(v, parameter.getItems()))
            .filter(s -> s != null)
            .findFirst();
    if(optional.isPresent()) {
        return optional.get();
    } else {
        return null;
    }
}
 
Example #24
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testArrayPipes() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("array");
    parameter.setCollectionFormat("pipes");
    String param = "toto|tutu|titi";
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, param);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertArrayEquals(new String[] { "toto", "tutu", "titi" },
            ((List) result).toArray());
}
 
Example #25
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testArrayOther() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("array");
    parameter.setCollectionFormat("anyOtherValue");
    String param = "toto,tutu,titi";
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, param);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertArrayEquals(new String[] { "toto", "tutu", "titi" },
            ((List) result).toArray());
}
 
Example #26
Source File: AbstractSerializableParameterExtractorTest.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Test()
public void testDefault() {
    String name = "myName";
    SerializableParameter parameter = new QueryParameter();
    parameter.setName("myName");
    parameter.setType("string");
    String param = "toto";
    MultiMap params = MultiMap.caseInsensitiveMultiMap();
    params.add(name, param);

    Object result = abstractSerializableParameterExtractor.extract(name, parameter, params);
    Assert.assertEquals("toto", result);
}
 
Example #27
Source File: StringParameterValidator.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
@Override
protected Status doValidate(
        final String value,
        final SerializableParameter parameter) {
    // TODO: Check pattern etc.
    return null;
}
 
Example #28
Source File: ValidatorTestUtil.java    From light-rest-4j with Apache License 2.0 4 votes vote down vote up
public static SerializableParameter floatParam() {
    return floatParam(true, null, null);
}
 
Example #29
Source File: RestControllerProcessor.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the given Java parameter type into a Swagger param type and sets it into the given Swagger param.
 *
 * @param parameterSource the parameter source.
 * @param swaggerParam the Swagger parameter.
 */
private void setParameterType(ParameterSource<JavaClassSource> parameterSource, SerializableParameter swaggerParam) throws MojoExecutionException
{
    try
    {
        String typeName = parameterSource.getType().getQualifiedName();

        if (String.class.getName().equals(typeName))
        {
            swaggerParam.setType("string");
        }
        else if (Integer.class.getName().equals(typeName) || Long.class.getName().equals(typeName))
        {
            swaggerParam.setType("integer");
        }
        else if (Boolean.class.getName().equals(typeName))
        {
            swaggerParam.setType("boolean");
        }
        else
        {
            // See if the type is an enum.
            Enum<?>[] enumValues = (Enum<?>[]) Class.forName(parameterSource.getType().getQualifiedName()).getEnumConstants();
            if (enumValues != null)
            {
                swaggerParam.setType("string");
                swaggerParam.setEnum(new ArrayList<>());
                for (Enum<?> enumEntry : enumValues)
                {
                    swaggerParam.getEnum().add(enumEntry.name());
                }
            }
            else
            {
                // Assume "string" for all other types since everything is ultimately a string.
                swaggerParam.setType("string");
            }
        }

        log.debug("Parameter \"" + parameterSource.getName() + "\" is a type \"" + swaggerParam.getType() + "\".");
    }
    catch (ClassNotFoundException e)
    {
        throw new MojoExecutionException("Unable to instantiate class \"" + parameterSource.getType().getQualifiedName() + "\". Reason: " + e.getMessage(),
            e);
    }
}
 
Example #30
Source File: SwaggerConverter.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
private Schema convertFormDataToSchema(io.swagger.models.parameters.Parameter formParam) {
    SerializableParameter sp = (SerializableParameter) formParam;
    return convert(sp);
}