use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.
the class ParameterSerializationTest method deserializeQueryParameter.
@Test(description = "it should deserialize a QueryParameter")
public void deserializeQueryParameter() throws IOException {
final String json = "{\"in\":\"query\",\"required\":false,\"type\":\"string\"}";
final Parameter p = m.readValue(json, Parameter.class);
SerializationMatchers.assertEqualsToJson(p, json);
}
use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.
the class ParameterProcessorTest method parameterProcessorTest.
@Test(description = "parse parameters from method")
public void parameterProcessorTest() throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod("parametrizedMethod", String.class, List.class, String.class, String.class, Integer.class);
final Type[] genericParameterTypes = method.getGenericParameterTypes();
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
final PathParameter p1 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter(), genericParameterTypes[0], Arrays.asList(paramAnnotations[0]));
assertNotNull(p1);
assertEquals(p1.getIn(), "path");
assertEquals(p1.getName(), "paramName1");
assertEquals(p1.getDescription(), "paramValue1");
assertEquals(p1.getDefaultValue(), "value1");
assertTrue(p1.getRequired());
assertEquals(p1.getEnum(), Arrays.asList("one", "two", "three"));
assertNull(p1.getAccess());
final QueryParameter p2 = (QueryParameter) ParameterProcessor.applyAnnotations(null, new QueryParameter().items(new IntegerProperty()), genericParameterTypes[1], Arrays.asList(paramAnnotations[1]));
assertNotNull(p2);
final IntegerProperty items = (IntegerProperty) p2.getItems();
assertNotNull(items);
assertEquals(p2.getIn(), "query");
assertEquals(p2.getName(), "paramName2");
assertNull(p2.getDescription());
assertEquals((int) items.getDefault(), 10);
assertFalse(p2.getRequired());
assertEquals(p2.getAccess(), "test");
final Parameter p3 = ParameterProcessor.applyAnnotations(null, null, genericParameterTypes[2], Arrays.asList(paramAnnotations[2]));
assertNull(p3);
final Parameter p4 = ParameterProcessor.applyAnnotations(null, null, genericParameterTypes[3], Arrays.asList(paramAnnotations[3]));
assertNull(p4);
final BodyParameter p5 = (BodyParameter) ParameterProcessor.applyAnnotations(null, null, genericParameterTypes[4], Arrays.asList(paramAnnotations[4]));
assertNotNull(p5);
assertEquals(p5.getIn(), "body");
}
use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.
the class ParameterDeserializer method deserialize.
@Override
public Parameter deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Parameter result = null;
JsonNode node = jp.getCodec().readTree(jp);
JsonNode sub = node.get("$ref");
JsonNode inNode = node.get("in");
if (sub != null) {
result = Json.mapper().convertValue(sub, RefParameter.class);
} else if (inNode != null) {
String in = inNode.asText();
if ("query".equals(in)) {
result = Json.mapper().convertValue(node, QueryParameter.class);
} else if ("header".equals(in)) {
result = Json.mapper().convertValue(node, HeaderParameter.class);
} else if ("path".equals(in)) {
result = Json.mapper().convertValue(node, PathParameter.class);
} else if ("formData".equals(in)) {
result = Json.mapper().convertValue(node, FormParameter.class);
} else if ("body".equals(in)) {
result = Json.mapper().convertValue(node, BodyParameter.class);
} else if ("cookie".equals(in)) {
result = Json.mapper().convertValue(node, CookieParameter.class);
}
}
return result;
}
use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.
the class SpecFilter method removeBrokenReferenceDefinitions.
private Swagger removeBrokenReferenceDefinitions(Swagger swagger) {
if (swagger.getDefinitions() == null || swagger.getDefinitions().isEmpty())
return swagger;
Set<String> referencedDefinitions = new TreeSet<String>();
if (swagger.getResponses() != null) {
for (Response response : swagger.getResponses().values()) {
String propertyRef = getPropertyRef(response.getSchema());
if (propertyRef != null) {
referencedDefinitions.add(propertyRef);
}
}
}
if (swagger.getParameters() != null) {
for (Parameter p : swagger.getParameters().values()) {
if (p instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) p;
Set<String> modelRef = getModelRef(bp.getSchema());
if (modelRef != null) {
referencedDefinitions.addAll(modelRef);
}
}
}
}
if (swagger.getPaths() != null) {
for (Path path : swagger.getPaths().values()) {
if (path.getParameters() != null) {
for (Parameter p : path.getParameters()) {
if (p instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) p;
Set<String> modelRef = getModelRef(bp.getSchema());
if (modelRef != null) {
referencedDefinitions.addAll(modelRef);
}
}
}
}
if (path.getOperations() != null) {
for (Operation op : path.getOperations()) {
if (op.getResponses() != null) {
for (Response response : op.getResponses().values()) {
String propertyRef = getPropertyRef(response.getSchema());
if (propertyRef != null) {
referencedDefinitions.add(propertyRef);
}
}
}
if (op.getParameters() != null) {
for (Parameter p : op.getParameters()) {
if (p instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) p;
Set<String> modelRef = getModelRef(bp.getSchema());
if (modelRef != null) {
referencedDefinitions.addAll(modelRef);
}
}
}
}
}
}
}
}
if (swagger.getDefinitions() != null) {
Set<String> nestedReferencedDefinitions = new TreeSet<String>();
for (String ref : referencedDefinitions) {
locateReferencedDefinitions(ref, nestedReferencedDefinitions, swagger);
}
referencedDefinitions.addAll(nestedReferencedDefinitions);
swagger.getDefinitions().keySet().retainAll(referencedDefinitions);
}
return swagger;
}
use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.
the class SpecFilter method filterOperation.
public Operation filterOperation(SwaggerSpecFilter filter, Operation op, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
Operation clonedOperation = new Operation().summary(op.getSummary()).description(op.getDescription()).operationId(op.getOperationId()).schemes(op.getSchemes()).consumes(op.getConsumes()).produces(op.getProduces()).tags(op.getTags()).externalDocs(op.getExternalDocs()).vendorExtensions(op.getVendorExtensions()).deprecated(op.isDeprecated());
List<Parameter> clonedParams = new ArrayList<Parameter>();
if (op.getParameters() != null) {
for (Parameter param : op.getParameters()) {
if (filter.isParamAllowed(param, op, api, params, cookies, headers)) {
clonedParams.add(param);
}
}
}
clonedOperation.setParameters(clonedParams);
clonedOperation.setSecurity(op.getSecurity());
clonedOperation.setResponses(op.getResponses());
return clonedOperation;
}
Aggregations