private HttpServiceActionTransferObject ReadAction(HttpServiceActionTypeTransferObject actionType, Dictionary <IEdmType, TypeTransferObject> mapping, IEdmNavigationSource navigationSource) { HttpServiceActionTransferObject action = new HttpServiceActionTransferObject { Name = actionType.ToString(), Type = actionType, Route = "", RequireBodyParameter = false }; switch (actionType) { case HttpServiceActionTypeTransferObject.Get: action.Parameters.Add(new HttpServiceActionParameterTransferObject { Name = "query", Type = new TypeTransferObject { Name = "Edm.String" }, AppendName = false }); action.ReturnType = this.ToTransferObject(navigationSource.Type, mapping); break; case HttpServiceActionTypeTransferObject.Post: case HttpServiceActionTypeTransferObject.Put: action.Parameters.Add(new HttpServiceActionParameterTransferObject { Name = "model", FromBody = true, Type = this.ToTransferObject(navigationSource.Type, mapping, false) }); action.ReturnType = new TypeTransferObject { Name = "Edm.Void" }; action.RequireBodyParameter = true; break; case HttpServiceActionTypeTransferObject.Patch: this.AddKeysToParameters(mapping, navigationSource, action); action.Parameters.Add(new HttpServiceActionParameterTransferObject { Name = "model", FromBody = true, Type = this.ToTransferObject(navigationSource.Type, mapping, false) }); action.ReturnType = new TypeTransferObject { Name = "Edm.Void" }; action.RequireBodyParameter = true; break; case HttpServiceActionTypeTransferObject.Delete: this.AddKeysToParameters(mapping, navigationSource, action); action.ReturnType = new TypeTransferObject { Name = "Edm.Void" }; break; } return(action); }
private void Read(OpenApiReadConfiguration configuration, OpenApiDocument document) { HttpServiceTransferObject service = new HttpServiceTransferObject { Name = configuration.Name ?? document.Info.Description.Replace("Class ", "").Trim() }; foreach (KeyValuePair <string, OpenApiPathItem> pathPair in document.Paths) { foreach (KeyValuePair <OperationType, OpenApiOperation> operationPair in pathPair.Value.Operations) { HttpServiceActionTypeTransferObject type = operationPair.Key.ToActionType(); HttpServiceActionTransferObject action = new HttpServiceActionTransferObject { Name = pathPair.Key.Split('/').Last(), Route = pathPair.Key, Type = type, RequireBodyParameter = type.IsBodyParameterRequired() }; foreach (OpenApiParameter parameter in operationPair.Value.Parameters) { action.Parameters.Add(new HttpServiceActionParameterTransferObject { Name = parameter.Name, Type = this.Read(configuration, parameter.Schema) }); } OpenApiMediaType content = operationPair.Value.RequestBody.Content.Single().Value; if (action.RequireBodyParameter) { action.Parameters.Add(new HttpServiceActionParameterTransferObject { Name = "request", Type = this.Read(configuration, content.Schema), FromBody = true }); } else { foreach (KeyValuePair <string, OpenApiSchema> propertyPair in content.Schema.Properties) { action.Parameters.Add(new HttpServiceActionParameterTransferObject { Name = propertyPair.Key, Type = this.Read(configuration, propertyPair.Value) }); } } OpenApiResponse response = operationPair.Value.Responses.SingleOrDefault(x => x.Key == "200").Value; if (response == null) { action.ReturnType = new TypeTransferObject { Name = "void" }; } else { action.ReturnType = this.Read(configuration, response.Content.Single().Value.Schema); } service.Actions.Add(action); } } }
public static bool IsBodyParameterRequired(this HttpServiceActionTypeTransferObject type) { return(type == HttpServiceActionTypeTransferObject.Post || type == HttpServiceActionTypeTransferObject.Patch || type == HttpServiceActionTypeTransferObject.Put); }