use of io.swagger.models.ArrayModel in project swagger-core by swagger-api.
the class PostParamTest method findPostOperationWithObjectsList.
@Test(description = "find a Post operation with list of objects")
public void findPostOperationWithObjectsList() {
Path petPath = getPath("listOfObjects");
assertNotNull(petPath);
Operation petPost = petPath.getPost();
assertNotNull(petPost);
assertEquals(petPost.getParameters().size(), 1);
BodyParameter petPostBodyParam = (BodyParameter) petPost.getParameters().get(0);
assertEquals(petPostBodyParam.getName(), BODY);
Model inputModel = petPostBodyParam.getSchema();
assertTrue(inputModel instanceof ArrayModel);
ArrayModel ap = (ArrayModel) inputModel;
Property inputSchema = ap.getItems();
assertTrue(inputSchema instanceof RefProperty);
RefProperty rm = (RefProperty) inputSchema;
assertEquals(rm.getSimpleRef(), PET);
}
use of io.swagger.models.ArrayModel in project swagger-core by swagger-api.
the class GenericsTest method check2DArrayAsBodyParameter.
@Test(description = "check 2D array as body parameter")
public void check2DArrayAsBodyParameter() {
Operation op = getOperation("test2DInBody");
assertEquals(op.getParameters().size(), 1);
BodyParameter p = getBodyParameter(op, 0);
ArrayModel ddArray = (ArrayModel) p.getSchema();
assertEquals(((RefProperty) ((ArrayProperty) ddArray.getItems()).getItems()).getSimpleRef(), "Tag");
}
use of io.swagger.models.ArrayModel in project swagger-core by swagger-api.
the class GenericsTest method checkCollectionsOfObjectsAsBodyParameter.
@Test(description = "check collection of objects as body parameter")
public void checkCollectionsOfObjectsAsBodyParameter() {
Operation op = getOperation("testObjectsInBody");
assertEquals(op.getParameters().size(), 1);
BodyParameter p = getBodyParameter(op, 0);
ArrayModel objArray = (ArrayModel) p.getSchema();
assertEquals(((RefProperty) objArray.getItems()).getSimpleRef(), "Tag");
}
use of io.swagger.models.ArrayModel in project swagger-core by swagger-api.
the class ParameterSerializationTest method serializeArrayBodyParameter.
@Test(description = "it should serialize an array BodyParameter")
public void serializeArrayBodyParameter() {
final ArrayModel model = new ArrayModel().items(new RefProperty("Cat"));
final BodyParameter p = new BodyParameter().schema(model);
final String json = "{" + " \"in\":\"body\"," + " \"required\":false," + " \"schema\":{" + " \"type\":\"array\"," + " \"items\":{" + " \"$ref\":\"#/definitions/Cat\"" + " }" + " }" + "}";
SerializationMatchers.assertEqualsToJson(p, json);
}
use of io.swagger.models.ArrayModel in project camel by apache.
the class RestSwaggerReader method doParseVerbs.
private void doParseVerbs(Swagger swagger, RestDefinition rest, String camelContextId, List<VerbDefinition> verbs, String pathAsTag) {
// used during gathering of apis
List<Path> paths = new ArrayList<>();
String basePath = rest.getPath();
for (VerbDefinition verb : verbs) {
// check if the Verb Definition must be excluded from documentation
Boolean apiDocs;
if (verb.getApiDocs() != null) {
apiDocs = verb.getApiDocs();
} else {
// fallback to option on rest
apiDocs = rest.getApiDocs();
}
if (apiDocs != null && !apiDocs) {
continue;
}
// the method must be in lower case
String method = verb.asVerb().toLowerCase(Locale.US);
// operation path is a key
String opPath = SwaggerHelper.buildUrl(basePath, verb.getUri());
Operation op = new Operation();
if (ObjectHelper.isNotEmpty(pathAsTag)) {
// group in the same tag
op.addTag(pathAsTag);
}
final String routeId = verb.getRouteId();
final String operationId = Optional.ofNullable(rest.getId()).orElse(routeId);
op.operationId(operationId);
// add id as vendor extensions
op.getVendorExtensions().put("x-camelContextId", camelContextId);
op.getVendorExtensions().put("x-routeId", routeId);
Path path = swagger.getPath(opPath);
if (path == null) {
path = new Path();
paths.add(path);
}
path = path.set(method, op);
String consumes = verb.getConsumes() != null ? verb.getConsumes() : rest.getConsumes();
if (consumes != null) {
String[] parts = consumes.split(",");
for (String part : parts) {
op.addConsumes(part);
}
}
String produces = verb.getProduces() != null ? verb.getProduces() : rest.getProduces();
if (produces != null) {
String[] parts = produces.split(",");
for (String part : parts) {
op.addProduces(part);
}
}
if (verb.getDescriptionText() != null) {
op.summary(verb.getDescriptionText());
}
for (RestOperationParamDefinition param : verb.getParams()) {
Parameter parameter = null;
if (param.getType().equals(RestParamType.body)) {
parameter = new BodyParameter();
} else if (param.getType().equals(RestParamType.formData)) {
parameter = new FormParameter();
} else if (param.getType().equals(RestParamType.header)) {
parameter = new HeaderParameter();
} else if (param.getType().equals(RestParamType.path)) {
parameter = new PathParameter();
} else if (param.getType().equals(RestParamType.query)) {
parameter = new QueryParameter();
}
if (parameter != null) {
parameter.setName(param.getName());
parameter.setDescription(param.getDescription());
parameter.setRequired(param.getRequired());
// set type on parameter
if (parameter instanceof SerializableParameter) {
SerializableParameter serializableParameter = (SerializableParameter) parameter;
if (param.getDataType() != null) {
serializableParameter.setType(param.getDataType());
if (param.getDataType().equalsIgnoreCase("array")) {
if (param.getArrayType() != null) {
if (param.getArrayType().equalsIgnoreCase("string")) {
serializableParameter.setItems(new StringProperty());
}
if (param.getArrayType().equalsIgnoreCase("int") || param.getArrayType().equalsIgnoreCase("integer")) {
serializableParameter.setItems(new IntegerProperty());
}
if (param.getArrayType().equalsIgnoreCase("long")) {
serializableParameter.setItems(new LongProperty());
}
if (param.getArrayType().equalsIgnoreCase("float")) {
serializableParameter.setItems(new FloatProperty());
}
if (param.getArrayType().equalsIgnoreCase("double")) {
serializableParameter.setItems(new DoubleProperty());
}
if (param.getArrayType().equalsIgnoreCase("boolean")) {
serializableParameter.setItems(new BooleanProperty());
}
}
}
}
if (param.getCollectionFormat() != null) {
serializableParameter.setCollectionFormat(param.getCollectionFormat().name());
}
if (param.getAllowableValues() != null && !param.getAllowableValues().isEmpty()) {
serializableParameter.setEnum(param.getAllowableValues());
}
}
// set default value on parameter
if (parameter instanceof AbstractSerializableParameter) {
AbstractSerializableParameter qp = (AbstractSerializableParameter) parameter;
if (param.getDefaultValue() != null) {
qp.setDefaultValue(param.getDefaultValue());
}
}
// set schema on body parameter
if (parameter instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) parameter;
if (verb.getType() != null) {
if (verb.getType().endsWith("[]")) {
String typeName = verb.getType();
typeName = typeName.substring(0, typeName.length() - 2);
Property prop = modelTypeAsProperty(typeName, swagger);
if (prop != null) {
ArrayModel arrayModel = new ArrayModel();
arrayModel.setItems(prop);
bp.setSchema(arrayModel);
}
} else {
String ref = modelTypeAsRef(verb.getType(), swagger);
if (ref != null) {
bp.setSchema(new RefModel(ref));
}
}
}
}
op.addParameter(parameter);
}
}
// if we have an out type then set that as response message
if (verb.getOutType() != null) {
Response response = new Response();
Property prop = modelTypeAsProperty(verb.getOutType(), swagger);
response.setSchema(prop);
response.setDescription("Output type");
op.addResponse("200", response);
}
// enrich with configured response messages from the rest-dsl
doParseResponseMessages(swagger, verb, op);
// add path
swagger.path(opPath, path);
}
}
Aggregations