@Test(description = "it should deserialize a path parameter with enum") public void deserializeEnumPathParameter() throws IOException { final String json = "{" + " \"in\":\"path\"," + " \"required\":true," + " \"schema\":{" + " \"type\":\"array\"," + " \"items\":{" + " \"type\":\"string\"," + " \"enum\":[\"a\",\"b\",\"c\"]" + " }" + "}}"; final Parameter p = m.readValue(json, Parameter.class); SerializationMatchers.assertEqualsToJson(p, json); ArraySchema as = (ArraySchema) p.getSchema(); assertEquals(((StringSchema) as.getItems()).getEnum(), Arrays.asList("a", "b", "c")); }
@Test(description = "it should deserialize a number path parameter with enum") public void deserializeNumberEnumPathParameter() throws IOException { final String json = "{" + " \"in\":\"path\"," + " \"required\":true," + " \"schema\":{" + " \"type\":\"array\"," + " \"items\":{" + " \"type\":\"integer\"," + " \"format\":\"int32\"," + " \"enum\":[1,2,3]" + " }" + " }" + "}"; final Parameter p = m.readValue(json, Parameter.class); SerializationMatchers.assertEqualsToJson(p, json); assertEquals(((IntegerSchema) ((ArraySchema) p.getSchema()).getItems()).getEnum(), Arrays.asList(1, 2, 3)); }
@Test(description = "it should desserialize Long schema correctly") public void deserializeLongSchema() throws IOException { final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/oas3_2.yaml"); final OpenAPI swagger = Yaml.mapper().readValue(jsonString, OpenAPI.class); assertNotNull(swagger); Schema s = swagger.getPaths().get("/withIntegerEnum/{stage}").getGet().getParameters().get(0).getSchema(); assertEquals(s.getEnum().get(0), 2147483647); assertEquals(s.getEnum().get(1), 3147483647L); assertEquals(s.getEnum().get(2), 31474836475505055L); }
if (StringUtils.isBlank(requestBody.get$ref()) && (requestBody.getContent() == null || requestBody.getContent().isEmpty())) { if (requestBodyParameter.getSchema() != null) { Content content = processContent(requestBody.getContent(), requestBodyParameter.getSchema(), methodConsumes, classConsumes); requestBody.setContent(content); requestBody.getContent() != null && !requestBody.getContent().isEmpty()) { if (requestBodyParameter.getSchema() != null) { for (MediaType mediaType : requestBody.getContent().values()) { if (mediaType.getSchema() == null) { if (requestBodyParameter.getSchema() == null) { mediaType.setSchema(new Schema()); } else { mediaType.setSchema(requestBodyParameter.getSchema()); mediaType.getSchema().setType(requestBodyParameter.getSchema().getType()); if (requestBodyParameter.getSchema() != null) { Content content = processContent(null, requestBodyParameter.getSchema(), methodConsumes, classConsumes); requestBody.setContent(content); isRequestBodyEmpty = false;
Optional<Content> content = AnnotationsUtils.getContent(p.content(), classTypes, methodTypes, parameter.getSchema(), null, jsonViewAnnotation); if (content.isPresent()) { parameter.setContent(content.get()); if (parameter.getSchema() == null) { parameter.setSchema(new ArraySchema()); if (parameter.getSchema() instanceof ArraySchema) { ArraySchema as = (ArraySchema) parameter.getSchema(); Integer min = (Integer) annotation.annotationType().getMethod("min").invoke(annotation); if (min != null) { Schema paramSchema = parameter.getSchema(); if (paramSchema == null) { if (parameter.getContent() != null && parameter.getContent().values().size() > 0) {
private void addPathItemSchemaRef(PathItem pathItem, Set<String> referencedDefinitions) { if (pathItem.getParameters() != null) { for (Parameter parameter : pathItem.getParameters()) { addSchemaRef(parameter.getSchema(), referencedDefinitions); addContentSchemaRef(parameter.getContent(), referencedDefinitions); addSchemaRef(parameter.getSchema(), referencedDefinitions); addContentSchemaRef(parameter.getContent(), referencedDefinitions);
Schema mergedSchema = new ObjectSchema(); for (Parameter formParam: formParameters) { mergedSchema.addProperties(formParam.getName(), formParam.getSchema());
/** * Retrieves the default value of a parameter * * @return the default value of the parameter */ public Optional<Object> getDefaultValue() { Validate.notNull(parameter, "parameter must not be null!"); if (parameter.get$ref() == null) { return Optional.ofNullable(parameter.getSchema()).map(Schema::getDefault); } return Optional.empty(); } }
private ValidationReport validateMinItems(final Collection<String> values, final Parameter parameter) { if (parameter.getSchema().getMinItems() != null && values.size() < parameter.getSchema().getMinItems()) { return ValidationReport.singleton(messages.get("validation.request.parameter.collection.tooFewItems", parameter.getName(), parameter.getSchema().getMinItems(), values.size()) ); } return ValidationReport.empty(); }
private ValidationReport validateMaxItems(final Collection<String> values, final Parameter parameter) { if (parameter.getSchema().getMaxItems() != null && values.size() > parameter.getSchema().getMaxItems()) { return ValidationReport.singleton(messages.get("validation.request.parameter.collection.tooManyItems", parameter.getName(), parameter.getSchema().getMaxItems(), values.size()) ); } return ValidationReport.empty(); }
private ValidationReport validateUniqueItems(final Collection<String> values, final Parameter parameter) { if (TRUE.equals(parameter.getSchema().getUniqueItems()) && values.stream().distinct().count() != values.size()) { return ValidationReport.singleton(messages.get("validation.request.parameter.collection.duplicateItems", parameter.getName()) ); } return ValidationReport.empty(); }
protected void updateLocalRefs(Parameter param, String pathRef) { if(param.getSchema() != null) { updateLocalRefs(param.getSchema(), pathRef); } if(param.getContent() != null) { Map<String, MediaType> content = param.getContent(); for (String key: content.keySet()) { MediaType mediaType = content.get(key); if (mediaType.getSchema() != null) { updateLocalRefs(mediaType.getSchema(), pathRef); } } } }
protected void updateLocalRefs(Parameter param, String pathRef) { if(param.getSchema() != null) { updateLocalRefs(param.getSchema(), pathRef); } if(param.getContent() != null) { Map<String, MediaType> content = param.getContent(); for (String key: content.keySet()) { MediaType mediaType = content.get(key); if (mediaType.getSchema() != null) { updateLocalRefs(mediaType.getSchema(), pathRef); } } } }
private ValidationReport validateArrayParam(final Collection<String> values, final Parameter parameter) { final ValidationReport report = Stream.of( validateMaxItems(values, parameter), validateMinItems(values, parameter), validateUniqueItems(values, parameter) ).reduce(ValidationReport.empty(), ValidationReport::merge); if (parameter.getSchema().getEnum() != null && !parameter.getSchema().getEnum().isEmpty()) { final Set<String> enumValues = new HashSet<>(parameter.getSchema().getEnum()); return values.stream() .filter(v -> !enumValues.contains(v)) .map(v -> ValidationReport.singleton(messages.get("validation.request.parameter.enum.invalid", v, parameter.getName(), parameter.getSchema().getEnum()) )) .reduce(report, ValidationReport::merge); } return values.stream() .map(v -> schemaValidator.validate( v, ((ArraySchema) parameter.getSchema()).getItems(), "request.parameter") ) .reduce(report, ValidationReport::merge); }
protected void updateLocalRefs(Parameter param, String pathRef) { if(param.getSchema() != null) { updateLocalRefs(param.getSchema(), pathRef); } if(param.getContent() != null) { Map<String, MediaType> content = param.getContent(); for (String key: content.keySet()) { MediaType mediaType = content.get(key); if (mediaType.getSchema() != null) { updateLocalRefs(mediaType.getSchema(), pathRef); } } } }
public static Parameter changeObjectToAnyForJavaObjectBodyParameter(Type type, Parameter param) { JavaType javaType = objectMapper.constructType(type); param.schema(changeObjectToAnyForJavaObjectProperties(javaType, param.getSchema())); return param; }
protected <T> void updateLocalRefs(String file, T result) { if(result instanceof ApiResponse) { ApiResponse response = (ApiResponse) result; if (response.getContent() != null) { for (String mediaType : response.getContent().keySet()) { updateLocalRefs(file, response.getContent().get(mediaType).getSchema()); } } } if(result instanceof Parameter){ Parameter parameter = (Parameter)result; if (parameter.getSchema() != null){ updateLocalRefs(file,parameter.getSchema()); } } if(result instanceof Schema && ((Schema)(result)).get$ref() != null) { Schema prop = (Schema) result; updateLocalRefs(file, prop); } else if(result instanceof Schema) { Schema model = (Schema) result; updateLocalRefs(file, model); } }
@Override public RequestParameter transform(Parameter from) { RequestParameterInType inType = requestParameterInTypeResolver.resolve(from.getIn()); String name = from.getName(); boolean required = BooleanUtils.toBoolean(from.getRequired()); Schema swSchema = from.getSchema(); if (swSchema != null) { return new RequestParameter(inType, name, required, schemaTransformer.transform(swSchema)); } return new RequestParameter(inType, name, required); } }
static void processFormParam(Parameter formParameter, Operation operation, MethodDoc methodDoc, Components components) { Option<Method> methodOpt = ReflectionKit.loadMethod(methodDoc); if (methodOpt.isDefined()) { Method method = methodOpt.get(); Consumes methodConsumes = ReflectionUtils.getAnnotation(method, Consumes.class); Consumes classConsumes = ReflectionUtils.getAnnotation(method.getDeclaringClass(), Consumes.class); RequestBody requestBody = operation.getRequestBody() != null ? operation.getRequestBody() : new RequestBody(); requestBody.description(formParameter.getDescription()); if (!Strings.isNullOrEmpty(formParameter.getDescription())) { requestBody.setDescription(formParameter.getDescription()); } Content content = processContent(requestBody.getContent(), formParameter.getSchema(), methodConsumes, classConsumes, true); requestBody.setContent(content); setRequestBodyExamples(formParameter, requestBody); operation.setRequestBody(requestBody); } }
@Override public BallerinaParameter buildContext(Parameter parameter, OpenAPI openAPI) throws BallerinaOpenApiException { this.name = parameter.getName(); this.in = parameter.getIn(); this.description = parameter.getDescription(); this.required = parameter.getRequired(); this.deprecated = parameter.getDeprecated(); this.allowEmptyValue = parameter.getAllowEmptyValue(); this.ref = parameter.get$ref(); this.style = parameter.getStyle(); this.explode = parameter.getExplode(); this.allowReserved = parameter.getAllowReserved(); this.examples = parameter.getExamples(); this.example = parameter.getExample(); this.content = parameter.getContent(); this.extensions = parameter.getExtensions(); this.schema = new BallerinaSchema().buildContext(parameter.getSchema(), openAPI); return this; }