-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Description
Java Code:
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Swagger;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.IntegerProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.PropertyBuilder;
import io.swagger.models.properties.PropertyBuilder.PropertyId;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
Swagger swagger=new Swagger();
Map<String,Model> definitions=new HashMap<String, Model>();
ModelImpl model=new ModelImpl();
model.setType(ModelImpl.OBJECT);
definitions.put("Student", model);
Map<String,Property> properties=new HashMap<String, Property>();
Property property=new IntegerProperty();
property.setRequired(true);
properties.put("Id", property);
Map<PropertyBuilder.PropertyId, Object> map=new HashMap<PropertyBuilder.PropertyId, Object>();
map.put(PropertyId.DESCRIPTION, "Number Of Subject");
map.put(PropertyId.TYPE, "array");
map.put(PropertyId.MIN_ITEMS, 1);
map.put(PropertyId.MAX_ITEMS, 100);
ArrayProperty ar=(ArrayProperty) PropertyBuilder.build("array", "token", map);
RefProperty ref=new RefProperty();
ref.set$ref("subject");
ar.setItems(ref);
properties.put("subjects", ar);
model.setProperties(properties);
ModelImpl model1=new ModelImpl();
model1.setType(ModelImpl.OBJECT);
definitions.put("subject", model1);
Map<String,Property> properties1=new HashMap<String, Property>();
Property property1=new StringProperty();
property1.setRequired(true);
properties1.put("name", property1);
model1.setProperties(properties1);
swagger.setDefinitions(definitions);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
String s=mapper.writeValueAsString(swagger);
System.out.println(s);
}
}OutPut:
{
"swagger": "2.0",
"definitions": {
"subject": {
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
},
"Student": {
"type": "object",
"required": [
"Id"
],
"properties": {
"subjects": {
"type": "array",
"format": "token",
"description": "Number Of Subject",
"items": {
"$ref": "#/definitions/subject"
}
},
"Id": {
"type": "integer",
"format": "int32"
}
}
}
}
}Here I need to populate "minItems and maxItems" to the subjects.
Can you please help here.