- @override
Returns all APIOperations this object knows about.
Source
@override
List<APIOperation> documentOperations(PackagePathResolver resolver) {
var controllerCache = HTTPControllerBinder.binderForType(runtimeType);
var reflectedType = reflect(this).type;
var uri = reflectedType.location.sourceUri;
var fileUnit = parseDartFile(resolver.resolve(uri));
var classUnit = fileUnit.declarations
.where((u) => u is ClassDeclaration)
.map((cu) => cu as ClassDeclaration)
.firstWhere((ClassDeclaration classDecl) {
return classDecl.name.token.lexeme ==
MirrorSystem.getName(reflectedType.simpleName);
});
Map<Symbol, MethodDeclaration> methodMap = {};
classUnit.childEntities.forEach((child) {
if (child is MethodDeclaration) {
methodMap[new Symbol(child.name.token.lexeme)] = child;
}
});
return controllerCache.methodBinders.values.map((cachedMethod) {
var op = new APIOperation();
op.id = APIOperation.idForMethod(this, cachedMethod.methodSymbol);
op.method = cachedMethod.httpMethod.method;
op.consumes = acceptedContentTypes;
op.produces = [responseContentType];
op.responses = documentResponsesForOperation(op);
op.requestBody = documentRequestBodyForOperation(op);
// Add documentation comments
var methodDeclaration = methodMap[cachedMethod.methodSymbol];
if (methodDeclaration != null) {
var comment = methodDeclaration.documentationComment;
var tokens = comment?.tokens ?? [];
var lines =
tokens.map((t) => t.lexeme.trimLeft().substring(3).trim()).toList();
if (lines.length > 0) {
op.summary = lines.first;
}
if (lines.length > 1) {
op.description = lines.sublist(1, lines.length).join("\n");
}
}
bool usesFormEncodedData = op.method.toLowerCase() == "post" &&
acceptedContentTypes.any((ct) =>
ct.primaryType == "application" &&
ct.subType == "x-www-form-urlencoded");
op.parameters = [
cachedMethod.positionalParameters,
cachedMethod.optionalParameters,
controllerCache.propertyBinders
].expand((i) => i.toList()).map((param) {
var paramLocation =
_parameterLocationFromHTTPParameter(param.binding);
if (usesFormEncodedData &&
paramLocation == APIParameterLocation.query) {
paramLocation = APIParameterLocation.formData;
}
return new APIParameter()
..name = param.name
..required = param.isRequired
..parameterLocation = paramLocation
..schemaObject =
(new APISchemaObject.fromTypeMirror(param.boundValueType));
}).toList();
return op;
}).toList();
}