use of io.swagger.models.Model in project swagger-core by swagger-api.
the class SpecFilter method filter.
public Swagger filter(Swagger swagger, SwaggerSpecFilter filter, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
Swagger clone = new Swagger();
clone.info(swagger.getInfo()).tags(swagger.getTags() == null ? null : new ArrayList<Tag>(swagger.getTags())).host(swagger.getHost()).basePath(swagger.getBasePath()).schemes(swagger.getSchemes()).consumes(swagger.getConsumes()).produces(swagger.getProduces()).externalDocs(swagger.getExternalDocs()).vendorExtensions(swagger.getVendorExtensions());
final Set<String> filteredTags = new HashSet<String>();
final Set<String> allowedTags = new HashSet<String>();
for (String resourcePath : swagger.getPaths().keySet()) {
Path path = swagger.getPaths().get(resourcePath);
Map<String, Operation> ops = new HashMap<String, Operation>();
ops.put("get", path.getGet());
ops.put("head", path.getHead());
ops.put("put", path.getPut());
ops.put("post", path.getPost());
ops.put("delete", path.getDelete());
ops.put("patch", path.getPatch());
ops.put("options", path.getOptions());
Path clonedPath = new Path();
for (String key : ops.keySet()) {
Operation op = ops.get(key);
if (op != null) {
ApiDescription desc = new ApiDescription(resourcePath, key);
final Set<String> tags;
if (filter.isOperationAllowed(op, desc, params, cookies, headers)) {
clonedPath.set(key, filterOperation(filter, op, desc, params, cookies, headers));
tags = allowedTags;
} else {
tags = filteredTags;
}
if (op.getTags() != null) {
tags.addAll(op.getTags());
}
}
}
if (!clonedPath.isEmpty()) {
clone.path(resourcePath, clonedPath);
}
}
final List<Tag> tags = clone.getTags();
filteredTags.removeAll(allowedTags);
if (tags != null && !filteredTags.isEmpty()) {
for (Iterator<Tag> it = tags.iterator(); it.hasNext(); ) {
if (filteredTags.contains(it.next().getName())) {
it.remove();
}
}
if (clone.getTags().isEmpty()) {
clone.setTags(null);
}
}
Map<String, Model> definitions = filterDefinitions(filter, swagger.getDefinitions(), params, cookies, headers);
clone.setSecurityDefinitions(swagger.getSecurityDefinitions());
clone.setSecurity(swagger.getSecurity());
clone.setDefinitions(definitions);
// existing client filters directly implementing SwaggerSpecFilter.
if (filter instanceof AbstractSpecFilter) {
if (((AbstractSpecFilter) filter).isRemovingUnreferencedDefinitions()) {
clone = removeBrokenReferenceDefinitions(clone);
}
}
return clone;
}
use of io.swagger.models.Model in project swagger-core by swagger-api.
the class SpecFilter method filterDefinitions.
public Map<String, Model> filterDefinitions(SwaggerSpecFilter filter, Map<String, Model> definitions, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
if (definitions == null) {
return null;
}
Map<String, Model> clonedDefinitions = new LinkedHashMap<String, Model>();
for (String key : definitions.keySet()) {
Model definition = definitions.get(key);
Map<String, Property> clonedProperties = new LinkedHashMap<String, Property>();
if (definition.getProperties() != null) {
for (String propName : definition.getProperties().keySet()) {
Property property = definition.getProperties().get(propName);
if (property != null) {
boolean shouldInclude = filter.isPropertyAllowed(definition, property, propName, params, cookies, headers);
if (shouldInclude) {
clonedProperties.put(propName, property);
}
}
}
}
Model clonedModel = (Model) definition.clone();
if (clonedModel.getProperties() != null) {
clonedModel.getProperties().clear();
}
if (definition.getVendorExtensions() != null && clonedModel.getVendorExtensions() != null) {
clonedModel.getVendorExtensions().putAll(definition.getVendorExtensions());
}
clonedModel.setProperties(clonedProperties);
clonedDefinitions.put(key, clonedModel);
}
return clonedDefinitions;
}
use of io.swagger.models.Model in project swagger-core by swagger-api.
the class ModelResolver method resolveSubtypes.
private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConverterContext context) {
final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
if (types == null) {
return false;
}
/**
* As the introspector will find @JsonSubTypes for a child class that are present on its super classes, the
* code segment below will also run the introspector on the parent class, and then remove any sub-types that are
* found for the parent from the sub-types found for the child. The same logic all applies to implemented
* interfaces, and is accounted for below.
*/
removeSuperClassAndInterfaceSubTypes(types, bean);
int count = 0;
final Class<?> beanClass = bean.getClassInfo().getAnnotated();
for (NamedType subtype : types) {
final Class<?> subtypeType = subtype.getType();
if (!beanClass.isAssignableFrom(subtypeType)) {
continue;
}
final Model subtypeModel = context.resolve(subtypeType);
if (subtypeModel instanceof ModelImpl) {
final ModelImpl impl = (ModelImpl) subtypeModel;
// check if model name was inherited
if (impl.getName().equals(model.getName())) {
impl.setName(_typeNameResolver.nameForType(_mapper.constructType(subtypeType), TypeNameResolver.Options.SKIP_API_MODEL));
}
// remove shared properties defined in the parent
final Map<String, Property> baseProps = model.getProperties();
final Map<String, Property> subtypeProps = impl.getProperties();
if (baseProps != null && subtypeProps != null) {
for (Map.Entry<String, Property> entry : baseProps.entrySet()) {
if (entry.getValue().equals(subtypeProps.get(entry.getKey()))) {
subtypeProps.remove(entry.getKey());
}
}
}
impl.setDiscriminator(null);
ComposedModel child = new ComposedModel().parent(new RefModel(model.getName())).child(impl);
context.defineModel(impl.getName(), child, subtypeType, null);
++count;
}
}
return count != 0;
}
use of io.swagger.models.Model in project swagger-core by swagger-api.
the class ModelConverterTest method scanModelWithOffset.
@Test(description = "it tests a model with java offset")
public void scanModelWithOffset() throws IOException {
final Map<String, Model> models = readAll(ModelWithOffset.class);
assertEquals(models.size(), 1);
final Model model = models.get("ModelWithOffset");
Property property = model.getProperties().get("offset");
assertEquals(property.getType(), "string");
assertEquals(property.getFormat(), "date-time");
}
use of io.swagger.models.Model in project swagger-core by swagger-api.
the class ModelConverterTest method setReadOnly.
@Test(description = "it should set readOnly per #854")
public void setReadOnly() {
final Map<String, Model> schemas = readAll(JacksonReadonlyModel.class);
final ModelImpl model = (ModelImpl) schemas.get("JacksonReadonlyModel");
final Property prop = model.getProperties().get("count");
assertTrue(prop.getReadOnly());
}
Aggregations